Force method call on null variable to throw a NullPointerException [duplicate]
Asked Answered
I

4

8

Possible Duplicate:
How come invoking a (static) method on a null reference doesn’t throw NullPointerException?
Static fields on a null reference in Java

I tried the code from this old video:

class Impossible {
    public static void main(String[] args) {
        Thread t = null;
        System.out.println(t.currentThread().getName());
    }
}

Output: main

Well, just what the heck is that?! Does java.lang.Thread breach any the NullPointerException rule?

But what I'm most interested in it: How can I make that variable behave to throw a NullPointerException?

Indemnification answered 24/11, 2012 at 9:40 Comment(4)
I'm aware of this.Indemnification
If you want the fastest way to ensure an NPE is thrown just call the .getClass(); method. Had the advantage of not increasing branch count in code coverageSerf
@RefLibApi So you know it is not possible to throw a NPE but are asking how to throw a NPE... Not sure I follow you.Beanery
NPE gave us a nice answer, don't pick on the good man. btw I'm stubborn.Indemnification
D
10

Does java.lang.Thread breach any the NullPointerException rule?

No, reason for a NPE been thrown is not related to a class. It is related to an instance of that class, on which invocation is done. Also, it depends upon which type of method, or field you are accessing.

What is happening here is, currentThread() is a static method of Thread class. Which is boudn to a class, rather than an instance. So, even if you invoke it on a reference of Thread class, it is actually invoked on class name.

So,

Thread t = null
t.currentThread();

is actually invoked as: -

Thread.currentThread();

So, when accessing a static member through an object reference expression, only the declared type of the reference matters. This means that:

  • It doesn't matter if the reference is actually pointing to null, since no instance is required.

  • If the reference is not null, it doesn't matter what the type of the object the reference is pointing to, there is no dynamic dispatch.


How can I make that variable behave to throw a NullPointerException?

Well, the current print statement will never throw a NPE. The first part is already explained above. Now, let's move ahead.

Thread.currentThread();

The above invocation will never return null. It always returns the current thread instance. And in Java, you are always inside one or the other thread. Even when inside public static void main method, you are executing the Main Thread. So, currentThread can't be null.

And hence, further invocation: -

Thread.currentThread().getName();

will work fine, and return the name of current thread.

Dextro answered 24/11, 2012 at 9:42 Comment(2)
Alright, the second part made it clear for me, thanks. =(Indemnification
@RefLibApi.. I thought that is the thing which might be confusing you. You're welcome :)Dextro
M
3

currentThread() is a static method of class Thread. This means that it's not associated with any particular instance of the class, but with the class itself.

With this in mind, t.currentThread() is simply a different way of saying Thread.currentThread(). The value of t is not used at all, so it doesn't matter whether t is null.

Muhammad answered 24/11, 2012 at 9:43 Comment(0)
B
0

You can't throw a NullPointerException by calling a static method. So you need to call an instance method - any instance method would do, for example:

t.checkAccess();
t.getId();
t.getName();

etc.

Beanery answered 24/11, 2012 at 10:2 Comment(0)
K
0

If you see that code inside your development env (Eclipse / Netbeans) you would have seen at the first moment that currentThread() is written cursive, which clearly indicates that it is a static method (Class method).

Further, If you have checkstyle enabled, it will warn you to not call static methods on instances of an object.

The question is a bit of a Foul, because the Code formatting here cannot display static methods differently formatted.

And to finally answer the question: The Class Thread is never null, instances of an class can be null.

Kemper answered 24/11, 2012 at 16:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.