3
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?

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
Jigar Naik
  • 1,946
  • 5
  • 29
  • 60
  • 1
    The standard JVM does not give this information, unfortunately (In Groovy you have this information because it adds some extra control). To locate your error, you can split in multiple lines, assigning to temporary variables, and check the stacktrace line number. – Benoît Sep 23 '15 at 05:26
  • `isNull(object)` checks in catch block might be applicable to suffice your requirements. Just my understanding. – bsingh Sep 23 '15 at 05:45
  • This is to troubleshoot legacy code, which has very long methods i.e.1000+ lines. There are might be 10+ objects or multiple lines which might throw Null Pointer. – Jigar Naik Sep 23 '15 at 05:48
  • 1
    Useful reading: [Avoiding != null statements](http://stackoverflow.com/questions/271526/avoiding-null-statements-in-java) – Jean-François Corbett Sep 23 '15 at 06:39

1 Answers1

3

The NullPointerException should not be a part of normal application logic. It's thrown only to find bugs in your code. If you write getEmployee().getDept().getLocation().getContactPerson(), you should be absolutely sure that employee, dept, location exist. Thus you can modify your methods like this:

public Employee getEmployee() {
    if(employee == null) {
        // some business-level exception
        throw new EmployeeAbsentException(this.toString());
    }
    return employee;
}

// in Employee class
public Dept getDept() {
    if(dept == null) {
        throw new EmployeeHasNoDeptException(this);
    }
    return dept;
}

And so on. Of course you should have your own system of business-logic exceptions. You may reuse the same exception class or create several ones (probably subclassing some abstract BusinessLogicException). This way you can easily distinguish what's happening and display the corresponding messages to the user or log them to investigate bugs.

If you want to be able to check whether Employee exists without catching exceptions, add another method like this:

public boolean hasEmployee() {
    return employee != null;
}
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
  • Perfect !!! Instead of throwing EmployeeAbsentException(this.toString()); I can just put logger whenever the object is null to show parent object info. as below which will work for me. Thanks – Jigar Naik Sep 23 '15 at 06:08
  • 1
    Well, this is probably not the best solution. See: [Avoiding != null statements](http://stackoverflow.com/questions/271526/avoiding-null-statements-in-java) – Jean-François Corbett Sep 23 '15 at 06:38