Java Pitfalls - Nulls and NullPointerException

suggest change


The value null is the default value for an uninitialized value of a field whose type is a reference type.

NullPointerException (or NPE) is the exception that is thrown when you attempt to perform an inappropriate operation on the null object reference. Such operations include:

The most common root causes for NPEs:

Examples of commonly used methods that return null include:

There are strategies for avoiding unwanted NPEs, such as explicitly testing for null or using “Yoda Notation”, but these strategies often have the undesirable result of hiding problems in your code that really ought to be fixed.

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents