Prasad prap wrote:Remember null pointer exceptions are always due to no object creation.
"Always" is a little strong, Prasad. null can be assigned as well:
Output:
And, one should not be confused into thinking that "null" is somehow an illegal value for an object variable to have in it, or even pass to another object's methods. For example:
Gets you this:
You know, I would love to get my hands on the person who decided to name that one Null
PointerException. C/C++ programmers who learn Java all have to come to terms with the words, "there are no pointers in Java." (Well,
I had to, anyway.) Object variables with null in them are part of what a Java programmer has to understand. Tossing error messages with the
word "pointer" embedded in them at a new Java coder just adds to the confusion.