简体   繁体   中英

Object is Null even after instantiating it

I have a question regarding this code:

Object obj = null;
someMethod(obj);
System.out.println(obj.getId());
..
..
void someMethod(Object obj) {
    obj = new Object();
    obj.setId("Id1");
}

The above code is throwing NullPointerException . Can anyone tell me why is the object still null even after instantiating it inside the method?

The call someMethod(obj) can't change the value of the obj variable (since Java is a pass by value language), so obj is still null after the call, and System.out.println(obj.getId()); throws NullPointerException .

An alternative that would work :

Object obj = someMethod();
System.out.println(obj.getId());
..
..
Object someMethod() {
    obj = new Object();
    obj.setId("Id1");
    return obj;
}

Java is pass by value, and you pass the reference to an object, not the object itself. I think it is very important to know this since this will let you understand how Java works much much better, so let's go step by step on your code and see why it doesn't work the way you expect.

Object obj = null;
someMethod(obj);
void someMethod(Object obj) {

Here, obj reference is copied (not the object instance, only the reference to it) then passed to someMethod .

obj = new Object();

Here you're overwriting obj value but only in the scope of the method. Note that obj reference in the caller (outside someMethod ) has not changed, because it is a copy. After the method finishes, obj reference in this scope is discarded because we go out of scope, and you go back to the caller scope. In this scope, obj is still null . Thus

System.out.println(obj.getId());

obviously throws NullPointerException .

If you want to get the new reference from the method, you can return it and assign it in the caller scope, for example:

Object someMethod() {
    obj = new Object();
    // Do stuff with obj
    return obj;
}
Object obj = someMethod();

In Java you copy arguments when passing them to a method. When we are talking about objects, you pass copy of reference. So assigning object inside of your method -you are applying them to the copy of reference.

You need to wrap obj reference and pass wrapper inside, than use setter method to set obj reference.

Second solution would be to return you object from your method.

void someMethod(Object obj1) { // incoming obj1 with reference to obj
    obj1 = new Object();   // now when you do new Object() obj1 contains new reference
    obj1.setId("Id1"); // the object referred by the obj1 reference is updated 
}

Basically java uses pass by value. When you call the someMethod(obj) , you are just passing the reference to the obj as an argument and not the object itself.When inside the someMethod , the object variable is initialized and now the obj1 inside the somemethod now contains reference to some other object(not the one you sent from the calling class).

So when your program returns to the calling class, the obj there again refers to the original reference that was sent initially ie to a null object. Hence you get a nullpointerexception . Note that when a method is invoked, it gets allocated its own stack frame, hence the obj1 inside somemethod() and obj inside the calling class' stack are different when the call returns back.

You get a NullPointerException due to the fact that the value of the variable obj has not changed . When you call the method someMethod and pass obj to it, then you passed obj by value and not by reference .

Java passes method arguments by value: it makes a copy of the object that you gave as an argument as opposed to giving a reference to it to the method; any changes are made to the copied object (that you can then return and use), not to the actual object.

Java passes by value the reference to an object.

At this point

someMethod(obj);

no object was created so the reference value is null.

If you update the reference variable inside the method somemethod() and return it to the obj variable you will see that then both references point to the same object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM