Java - 如何在此 String 方法中返回 null?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21192788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:12:40  来源:igfitidea点击:

Java - how to return a null in this String method?

javaarraysobjectnullreturn

提问by AHA27

How to make this string return a null if we put things = null

如果我们放置,如何使这个字符串返回空值 things = null

public class Test {
static String concatAll(Object[] things) {

    StringBuffer result = new StringBuffer();
    for (int i = 0; i < things.length; i++) {
       result.append( things[i] );
    }
    String x = result.toString();
    return x;

}


public static void main(String [] args)
{


    Object[] obj = {19, "abc", " ", 3.5};
    System.out.println(concatAll(obj)); 
}

}

}

this code will print

此代码将打印

19abc 3.5

19 美国广播公司 3.5

when i put obj = nullin the main method this code is not working, Why?

当我放入obj = nullmain 方法时,此代码不起作用,为什么?

采纳答案by Ankur Shanbhag

Here it is:

这里是:

    static String concatAll(Object[] things) {
      if(null == things) {
        return null;
      }
     //Rest of the code goes here
    }

回答by Raju Rathore

    if(null!=things)
    {
     for (int i = 0; i < things.length; i++) {
           result.append( things[i] );
        }
        String x = result.toString();
        return x;
    }else {
    return null;
    }

回答by duffymo

I'd check to make sure that the array wasn't null before using it:

在使用数组之前,我会检查以确保数组不为空:

public class Test {

    static String concatAll(Object[] things) {
        StringBuffer result = new StringBuffer();
        if (things != null) {
            for (int i = 0; i < things.length; i++) {
                result.append( things[i] );
            }
        }
        return result.toString();
    }

    public static void main(String [] args) {
        Object[] obj = {19, "abc", " ", 3.5};
        System.out.println(concatAll(obj)); 
    }
}

I'd also recommend that you pay attention to formatting. Style matters.

我还建议您注意格式。风格很重要。

回答by Scott

Your code as it stands try's to index into the Objectarray without checking for null with the code things.length, which will throw a NullPointerExceptionbecause there is no array object in existence, so the length field also does not exist.

您现在的代码尝试对Object数组进行索引,而不使用代码检查 null things.length,这将抛出 a,NullPointerException因为不存在数组对象,因此长度字段也不存在。

To fix this use an if statement!

要解决此问题,请使用 if 语句!

static String concatAll(Object[] things) {
if(things == null) {
    return null;
}
...continue code....

This will first check thingsfor a null value BEFORE executing code that could throw an exception.

这将things在执行可能引发异常的代码之前首先检查空值。

回答by ansible

Calling a method on a null object causes a NullPointerException.

对空对象调用方法会导致NullPointerException.

In your case if obj = null, then when you call concatAll, things will be null as well. You then try and call things.length which is not valid on a null object.

在你的情况下,如果 obj = null,那么当你调用 concatAll 时,事情也会为 null。然后,您尝试调用对空对象无效的 things.length。

You can make this method safer by checking the input of strings first. What you want to do exactly will depend on your situation, but here is one example.

您可以通过首先检查字符串的输入来使此方法更安全。您究竟想做什么取决于您的情况,但这里有一个示例。

static String concatAll(Object[] things) {

    if (things == null)
    {
        return "";
    }

    StringBuffer result = new StringBuffer();
    for (int i = 0; i < things.length; i++) {
       result.append( things[i] );
    }
    String x = result.toString();
    return x;

}