NullPointerException problem
No. 243
Q: |
Consider the following method inside a class
mi
We observe the following run-time error at line 26: Exception in thread "main" java.lang.NullPointerException
at mi.NpeString.getLength(NpeString.java:26)
at ... What caused this NPE error? Provide a possible sample data array causing this exception. Explain the implementation error with respect to the provided Javadoc promise and propose a solution resolving the flaw. |
||
A: |
The Javadoc™ contract regarding our
method
The variable
The local variable static int getLength(final String[] strings) {
int length = 0;
for (final String s: strings) {
if (null != s) {
length += s.length();
}
}
return length;
} |