Why String combines null with the + operator and throws a NullPointerException using the concate () method

Here is my class where I am joining two strings. Concatenation string using null using the + operator runs smoothly, but throws a NullPointerException using the concate() method.

 public class Test { public static void main(String[] args) { String str="abc"; String strNull=null; System.out.println(strNull+str); str.concat(strNull); } } 

can someone tell me the reason for this?

+7
java string
source share
9 answers

Case 1:

  System.out.println(strNull+str); // will not give you exception 

From docs (string conversion)

If the reference is null, it is converted to the string "null" (four ASCII characters n, u, l, l).

Otherwise, the conversion is performed as if by calling the toString of the reference object with no arguments; but if the result of calling the toString method is null, then the string "null" is used instead.

Case 2:

 str.concat(strNull); //NullPointer exception 

If you see the source concat(String str) , it uses str.length(); , so it will look like null.length() , giving you a NullPointerException .

+7
source share

If you see java.lang.String in the source and use the null parameter as a parameter. NPE is cast in the first line method along the length ().

 public String concat(String str) { int otherLen = str.length();//This is where NullPointerException is thrown if (otherLen == 0) { return this; } getChars(0, count, buf, 0); str.getChars(0, otherLen, buf, count); return new String(0, count + otherLen, buf); } 
+6
source share

Since inside the concat method in the String class, the length method is called by the passed parameter, which causes NPE to be called.

 public String concat(String str) { int otherLen = str.length();<------ if (otherLen == 0) { return this; } int len = value.length; char buf[] = Arrays.copyOf(value, len + otherLen); str.getChars(buf, len); return new String(buf, true); } 
+2
source share

concate() creates a new String() object under the hood.

where as + combines \ combines values โ€‹โ€‹using the toString() method. Therefore, when we use + , these are the continents "somevalue".append(null).toString() .

for reference see this question.

+2
source share

String.concat() needs an object of type String as a parameter.

There is no type that is null is instanceof . Refer to JLS:

15.20.2 Type Comparison Instance Operator

Relationship Expression: RelationalExpressionof Instance of ReferenceType At run time, the result of the instanceof statement is true if the value of RelationalExpression is not null and the reference can be passed to ReferenceType without raising the ClassCastException class. Otherwise, the result will be false.

+2
source share

The actual implementation of the concate method is as follows. this method needs an object of type String as a parameter, and if the argument is null, it returns Null Pointer Exp.

 public String concat(String paramString) { int i = paramString.length(); if (i == 0) { return this; } int j = this.value.length; char[] arrayOfChar = Arrays.copyOf(this.value, j + i); paramString.getChars(arrayOfChar, j); return new String(arrayOfChar, true); } 
+2
source share

The String class contains an array of characters (possibly an ArrayList ). When you call .concat() , it goes through and adds each character from the second line to the first.

If the first line is null , there is nothing to add, raising a NullPointer Exception . Try initializing String with "" .

+1
source share

@Blip: 1) null + "ABC"

A direct answer to your question: + The operator in java uses the StringBuilder.append method to concatenate strings.

And the StringBuilder.append () method treats the null object as a "null" String. Therefore, null + "ABC" will not throw a Null Pointer exception, but rather prints nullABC.

2) String.concat ()

And yes, there is no null check before the string.length () method in the concat () function of the String class. Therefore, it throws a null pointer exception.

Hope this answers your question.

+1
source share

Initializing a null string with "" should work.

-3
source share

All Articles