can we call any method on null object?

2019-02-18 09:49发布

is it possible?

Object obj=null;

obj.someMethod();

someMethod{/*some code here*/}

10条回答
混吃等死
2楼-- · 2019-02-18 10:27

You can call a static method on a null pointer. The pointer will naturally be completely ignored in a static method call, but it's still a case when something that (without looking at the class definition) seemingly should cause a NullPointerException runs just fine.

class FooObject {
    public static void saySomething() {
        System.out.println("Hi there!");
    }
}

class Main {
    public static void main(String[] args) {
        FooObject foo = null;
        foo.saySomething();
    }
}

But just to make it clear - no, you can't call an instance method with a null pointer. Protecting the programmer against this is one of the really basic protections that set languages like Java apart from "lower level languages" such as C++. It enables the error to be reported at the calling end, instead of it causing an inexplicable segfault/whatnot inside the method itself.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-02-18 10:29

"obj" variable references to no instance on the heap. For that reason you'll get a nullpointerexception at runtime. Even your Java IDE (Eclipse, IDEA) will catch the problem and warns you about this.

查看更多
劳资没心,怎么记你
4楼-- · 2019-02-18 10:29

No, there is no way to call a method on a null reference (unless the method is static!). (null does not represent some "base" object, it represents a reference which does not point to any object at all.)

This works fine however (ideone.com link):

class MethodTest {
    static void someMethod() {
        System.out.println("Hello World");
    }
}

class Test {
    public static void main(String[] args) {
        MethodTest mt = null;
        mt.someMethod();
    }
}

Relevant quote from the JLS:

15.12.4.4 Locate Method to Invoke
The strategy for method lookup depends on the invocation mode.

[...]

If the invocation mode is static, no target reference is needed and overriding is not allowed. Method m of class T is the one to be invoked.

Otherwise, an instance method is to be invoked and there is a target reference. If the target reference is null, a NullPointerException is thrown at this point. Otherwise, the target reference is said to refer to a target object and will be used as the value of the keyword this in the invoked method.

[...]

查看更多
We Are One
5楼-- · 2019-02-18 10:35

This won't compile as Object doesn't have someMethod(). However if you are talking about something like

MyClass o = null;
o.someMethod();

the answer depends on whether someMethod is static or not. If it is static, the value is ignored and it doesn't matter if its null or not.

e.g.

Thread t = null;
t.yield();

runs fine without an exception.

查看更多
女痞
6楼-- · 2019-02-18 10:36

you cant execute

null.someMethod(); !!!

查看更多
萌系小妹纸
7楼-- · 2019-02-18 10:39

A null object does not exist. In your example, you have a variable (a pointer) that can either store a reference to an instance or nothing.

If it doesn't point to an instance - well, then we can't use it to call methods or access fields.


wait, wait - this compiles and runs:

Math m = null;
System.out.println(m.max(1,2)); 

We can call static methods and access static fields in any variable (we just have to ignore compiler/IDE warnings!) But that is something different, a static method/variable is not called/accessed on the instance but on the class itself.

查看更多
登录 后发表回答