What is NullPointerException?
NullPointerException is a RuntimeException . In Java, a special null value can be assigned to an object reference. NullPointerException is thrown when an application attempts to use an object reference that has the null value. These include: Calling an instance method on the object referred by a null reference.
Please click here to know in details : NullPointerException
Sample code for generating NullPointerException
package com.javaskool;
public class TestDrive {
public static void main(String[] args) {
String intPartLength = null;
try {
System.out.println(intPartLength.substring(5, 10));
} catch (NullPointerException npe) {
System.out.println("NullPointerException Error. Message: " + npe.getMessage());
npe.printStackTrace();
} catch (NumberFormatException nfe) {
System.out.println("NumberFormatException Error. Message: " + nfe.getMessage());
nfe.printStackTrace();
} catch (Exception e) {
System.out.println("General Error. Message: " + e.getMessage());
e.printStackTrace();
}
}
}
And the Error Message will be
NullPointerException Error. Message: null
java.lang.NullPointerException
at com.javaskool.TestDrive.main(TestDrive.java:9)
Recent Comments