1

Is there any way in java to know the name of the Class of the offending object when NullPointerException is thrown? This will help in debugging the code when the offending java statement has lot of objects which can be null.

Jay
  • 671
  • 9
  • 10

4 Answers4

4

Have a look at this code and comments:

public class Main {

    public static void main(final String args[])
    {
        Main test = new Main();
        test.testNull();
        System.out.println("My program still works!");  //this WILL NOT be printed asn NPE is thrown in testNull() and it's not caught - this will result in stopping the program.
    }

    private void testNull()
    {
        String aaa = null;
        aaa.charAt(1); //here you'll get a NullPointerException because there 'aaa' doesn't reference any existing object.
    }
}

Output would be like this:

Exception in thread "main" java.lang.NullPointerException  //error caught in method "main", you also have the exception class shown.
    at Main.testNull(Main.java:26)  //null was found in file Main.java in method testNull on line 26.
    at Main.main(Main.java:19)      //the method testNull was called from method main - line 19.

Edit: You can do it like that:

public static void main(final String args[])
{
    Main test = new Main();
    try
    {
        test.testNull();
    }
    catch (final NullPointerException e) //catch the NPE
    {
        e.printStackTrace();             //print info about the cause.
        //do some other stuff to handle the exception
    }
    System.out.println("My program still works!");  //this WILL be printed
}

Output:

java.lang.NullPointerException
    at Main.testNull(Main.java:25)
    at Main.main(Main.java:13)
My program still works!
Michał Schielmann
  • 1,372
  • 8
  • 17
1

Trying to untangle some of your wordings:

An object is never null! It is a reference that can be null. And a reference has a type - either it is a declared variable or it is a more complex expression (for example a method call).

A NullPointerException occurs when you try to dereference a reference that is null which simply means that the reference does not point to an object. There is no chance to get the class of an object that does not exist. I think, you wanted to retrieve the type of the expression that refers to null in case of that NPE, didn't you?

The only thing you can get (and should analyze) is the source code line where the NPE occurred. This is part of the exception's stack trace. With that information you can debug your code.

Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66
0

Unfortunatelly no.

You mean the Declaration-Type of the value. It is only available at Compile-Time (who are usually reported to the IDE).

After compilation into bytecode, the declaraion-informations are dropped so even a int can contain a String. This is why method-signatures are sooo important!

Grim
  • 1,938
  • 10
  • 56
  • 123
0

Adding to Seelenvirtuose's answer:

If you want to find out what exactly is causing the NullPointerException first locate the line in the stack trace that throws the exception. This is usually the top line in the trace.

Let's say it is this line:

vfoo = foo.ffoo(bar.fbar(),foo2);

The possible offenders here are foo or bar. vfoo can't throw the exception since it is not being dereferenced, same as foo 2. It can't be within the functions foo.ffoo() or bar.fbar() since then the stack trace wouldn't point to this line, but would show an exception within these functions. So everything that is left is foo and bar, since they are both dereferenced. So to find out which one of these it is, just separate the statements:

temp = bar.fbar();
vfoo = foo.ffoo(temp,foo2);

Now the stack trace will point to one or the other line and you will know that either bar or foo is null.

You can also do something like this before the statement:

System.out.println("bar exists: "+(bar!=null));
System.out.println("foo exists: "+(foo!=null));

Last point to talk about: When are variables dereferenced?

Only Objects can be dereferenced, so primitives like int, float, double, char, byte and there like can neither be Null nor can they be dereferenced. An Object is dereferenced, when you use the . operator to access an Object's fields or functions (e.g. foo.bar() or foo.ibar = 5). Also it is dereferenced when you use an Object that supports autounboxing (e.g. Integer, Float, Double, Char, ...) and use any of the mathematical or logical operators (e.g. +, -, *, /, &, &&, |, ||, !). It's basically every time you try to access the content of an Object and not only the reference to it.

Dakkaron
  • 5,930
  • 2
  • 36
  • 51