When checking a program, the compiler can detect runtime exceptions, i.e. exceptions like the NullPointerException
, which belongs to the essential package java.lang
of the Java programming language.
Here are all the situations where a NullPointerException
occurs that are directly* mentioned in the Java Language Specification:
public class NullPointerExceptionExample {
public static void main(String[] args) {
String str = null;
try {
System.out.println(str.length());
} catch (NullPointerException e) {
System.out.println("Null pointer access!");
System.out.println("Error: " + e);
}
}
}
Null pointer access!
Error: java.lang.NullPointerException