A NullPointerException is thrown when you call a method / variable from a reference that doesn't point anywhere.
See the following example class. In the main() method, we create a reference and an object and then set it to null. The reference e doesn't point anywhere when the method show() is called. Thus, JVM generates a NullPointerException.
public class example {
public void show() {
}
public static void main (
String args[]) {
example e = new example();
e = null;
e.show();
}
}
Muhammad Ali Shah
Karachi, Pakistan.