0

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!

3 Answers 3

2

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately, in the production source code, it was not commented out, therefore i am unable to understand why. :(
If the code is not commented out, another possibility is that someone change the value of ep before throwing the exception. I'd like to see the code Car.run().
2

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.