-3

How to make this string return a null if we put 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

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

AHA27
  • 31
  • 1
  • 3
  • 12
  • 1
    You should understand what a `NullPointerException` is : http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception – Alexis C. Jan 17 '14 at 18:04
  • "this code is not working" doesn't tell us what you expected or what the result was... or where you set `obj` to null. – Jon Skeet Jan 17 '14 at 18:05
  • so i want null as a return @JonSkeet , i set obj = null in main method – AHA27 Jan 17 '14 at 18:07

5 Answers5

2

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

To fix this use an if statement!

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

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

1

Calling a method on a null object causes a 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.

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;

}
ansible
  • 3,569
  • 2
  • 18
  • 29
0

Here it is:

    static String concatAll(Object[] things) {
      if(null == things) {
        return null;
      }
     //Rest of the code goes here
    }
Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38
  • @Arief27: Please accept the answer, if you think it has helped you. Read this to understand how accepting the answer works. http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Ankur Shanbhag Jan 17 '14 at 18:17
0
    if(null!=things)
    {
     for (int i = 0; i < things.length; i++) {
           result.append( things[i] );
        }
        String x = result.toString();
        return x;
    }else {
    return null;
    }
Raju Rathore
  • 149
  • 5
0

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.

duffymo
  • 305,152
  • 44
  • 369
  • 561