public class ExampleNullPointer {
public static void main(String[] args) {
try {
getEmployee().getDept().getLocation().getContactPerson();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
}
Output
java.lang.NullPointerException
at com.india.test.ExampleNullPointer.main(ExampleNullPointer.java:7)
Looking at above code and its output, when java throws java.lang.NullPointerException
, is there anyway we can wrap this NullPointerException
to show additional information, like which Object is null, or some info about its parent object, such as the parent object's name attributes?
I am mainly looking for parent object's attribute values, for example Dept is null so if I can log Employee's primary key or Employee's name by overriding toString()
method.
Is there anyway I can generically make changes to get these information, so that whenever program throws NullPointerException
I get these additional information which will help me easily identify the issue and the data which are causing the issue?