I met a quite strange case in a program with jdk1.8, that I got a null pointer exception in String.length (which is in StringBuilder.append()) method,
the exact position of the null pointer exception is here in String of jdk,
public int length() {
return value.length; //line 623
}
and it's called in AbstractStringBuilder class :
public AbstractStringBuilder append(String str) {
if (str == null)
return appendNull();
int len = str.length(); //here line 447
ensureCapacityInternal(count + len);
str.getChars(0, len, value, count);
count += len;
return this;
}
I can't understand how this can happen, as there is a null check before str.length() called.
And maybe due to the same reason, all the way I tried to print the string content crashed, so I can't get what string content caused this.
Now, what I want to know is what can cause this, can I build a string content to reproduce this? Thanks for any ideas.
the stacktrace
java.lang.NullPointerException
at java.lang.String.length(String.java:623)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:447)
at java.lang.StringBuilder.append(StringBuilder.java:141)
at ....toString(....java:14) //a toString method of some class
at java.lang.String.valueOf(String.java:2994)
at java.lang.StringBuilder.append(StringBuilder.java:136)
at java.util.AbstractCollection.toString(AbstractCollection.java:462)
at java.lang.String.valueOf(String.java:2994)
at java.lang.StringBuilder.append(StringBuilder.java:136)
at com
I got this in both these two enviroments:
java version "1.8.0_102" Java(TM) SE Runtime Environment (build 1.8.0_102-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)
and
openjdk version "1.8.0_345" OpenJDK Runtime Environment (Temurin)(build 1.8.0_345-b01) OpenJDK 64-Bit Server VM (Temurin)(build 25.345-b01, mixed mode)
AbstractStringBuilder? Where did you find the source code? There may be differences between jdk versions (both between, say, 1.8.1 and 1.8.100, and also between the same version but from different vendors, e.g. Oracle and Amazon). This may have made you look at the wrong line.String::length. And the only way for an NPE to be thrown on that line is for the internalchar[]ofStringto benull. I don't know how that can happen.AbstractStringBuilderis misleading. The problem isn't in that class'sappendmethod--it's in theString::lengthmethod. More specifically, the problem is that somehow aStringis being created with its internalvaluefield set tonull. But as far as I know that's not possible (except maybe through instrumentation/reflection). You will likely have to provide a minimal reproducible example (including exact Java version and vendor information) if you want help.