HJW
HJW

Reputation: 23443

Java Exception NullPointerException in Custom Exception

Somehow i came across a bug today and i was trying to replicate it.

I believe the below code will throw a NullPointerException because ep is not instantiated, but i am unable to simulate it..

I believe that the instance variable ep is null when the exception is thrown from the main. But running it doesn't show NullPointerException.

When the exception is thrown (throw e.getILException(100)), is the MyException.MyException() constructor called?

package sg.java.testException;

public class MyException extends Exception {

    Exception ep;

    public MyException() {
        super();
        ep = new Exception();
    }

    public OtherException getILException(long txnId) {
            // the error is here, according to the stacktrace
        return new OtherException(ep, ep.toString());
    }
}

Caller:

public static void main(String args[]) throws OtherException{
        try{
            Car.run();
        }catch (MyException e){
            throw e.getILException(100);
        }
    }

Car Codes:

public class Car {
    public static void run() throws MyException {
        throw new MyException();
    }
}

I want to be able to replicate this exception:

java.lang.NullPointerException
    at sg.java.testException.MyException.getILException(MyException.java:13)
    at sg.java.testException.ExceptionTest.main(ExceptionTest.java:8)

Thanks!

Upvotes: 0

Views: 4351

Answers (3)

wannik
wannik

Reputation: 12726

When the exception is thrown (throw e.getILException(100)), is the MyException.MyException() constructor called?

No. It is called before that. The statement throw e.getILException(100) invokes the method getILException(...) then throws whatever returned from the method.

I comment out the line ep = new Exception(); in MyException's constructor.

class OtherException extends Exception {
    OtherException(Exception ex, String exStr) {}
}

class MyException extends Exception {

    Exception ep;

    public MyException() {
        super();
        // ep = new Exception();
    }

    public OtherException getILException(long txnId) {
            // the error is here, according to the stacktrace
        return new OtherException(ep, ep.toString());
    }
}

public class Test {
    public static void main(String args[]) throws OtherException {
        try{
            throw new MyException();
        }catch (MyException e){
            throw e.getILException(100);
        }
    }
}

The following is the result.

Exception in thread "main" java.lang.NullPointerException
at MyException.getILException(Test.java:16)
at Test.main(Test.java:25)

Upvotes: 2

Jack Leow
Jack Leow

Reputation: 22487

Well, the behavior you describe would occur if MyException.ep is null. While the value is initialized during instantiation, it is a package private variable, which means some other class in sg.java.testException could potentially set ep to null.

I would look to see if you have any code in that package that may be doing that. An easy way to do this, if you have access to all the source code, is to simply make ep private or final. If you do this, you should start seeing compile errors where ep is being modified.

Upvotes: 2

Ashwinee K Jha
Ashwinee K Jha

Reputation: 9317

You will get the NPE only when your car.run() method causes an exception. Is that code path triggered?

One issue may be that MyException has other constructors that don't initialize ep, or it may be having a sub-class that does not have ep.

Upvotes: 1

Related Questions