java java中的空引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14017461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:44:58  来源:igfitidea点击:

Null reference in java

java

提问by Abhij

I have a piece of code for which i have to know how memory is allocated

我有一段代码,我必须知道如何分配内存

public class Demo {

public void checkNullReference(){
    ConsumerName name =  null;
    addReference(name);
    System.out.println(name.getConsumerName());
}

public void addReference(ConsumerName name){
    name = new ConsumerName();
    name.setConsumerName("KRISHNA");
}

public static void main(String []args){
    Demo demo = new Demo();
    demo.checkNullReference();
}
}

The code is giving null pointer exception i have given a refrence of object to method and there i am allocating new object to it and setting name if i rewrite the method then every thing is working as expected.

代码给出了空指针异常,我已经给方法一个对象的引用,我正在为它分配新对象并设置名称,如果我重写方法,那么一切都按预期工作。

public void checkNullReference(){
    ConsumerName name =  new ConsumerName();
    addReference(name);
    System.out.println(name.getConsumerName());
}

回答by Ted Hopp

You cannot change a reference in a calling method from the called method. Thus, with this code:

您不能从被调用的方法更改调用方法中的引用。因此,使用此代码:

public void checkNullReference(){
    ConsumerName name =  null;
    addReference(name);
    System.out.println(name.getConsumerName());
}

namewill still be nullafter the call to addReference(name), regardless of what addReferencedoes with its formal argument.

name仍然会null在调用 之后addReference(name),不管addReference它的形式参数是什么。

You can redesign addReferenceto returnan instance of ConsumerName. While you're at it, you can delete the argument, since it is ignored. The result could be:

您可以重新设计addReference,以返回的一个实例ConsumerName。在此期间,您可以删除该参数,因为它已被忽略。结果可能是:

public void checkNullReference(){
    ConsumerName name =  addReference();
    System.out.println(name.getConsumerName());
}

public ConsumerName addReference(){
    ConsumerName name = new ConsumerName();
    name.setConsumerName("KRISHNA");
    return name;
}

回答by Vamsi Mohan Jayanti

You are calling addReference() method with a null as input so .. there is no pass by reference happening and ConsumerName is newly getting allocated with in addReference() its scope will remain with in the method only. So you modify your code to return the new instance of ConsumerName .

您正在使用 null 作为输入调用 addReference() 方法,因此 .. 没有通过引用发生,并且 ConsumerName 是在 addReference() 中新分配的,其范围将仅保留在方法中。因此,您修改代码以返回 ConsumerName 的新实例。

public class Demo {

public void checkNullReference(){
    ConsumerName name =  null;
    name = addReference(name);
    System.out.println(name.getConsumerName());
}

public ConsumerName addReference(ConsumerName name){
    name = new ConsumerName();
    name.setConsumerName("KRISHNA");
    return name ;
}

public static void main(String []args){
    Demo demo = new Demo();
    demo.checkNullReference();
}
}

回答by Infinity

Always remember, java uses pass-by-value. When you do

永远记住,java 使用值传递。当你做

name = new ConsumerName();
name.setConsumerName("KRISHNA");

it just simply creates a new local object which locate inside addReference function stack. So, as soon as the function returned, you lost that object. In another word, your ConsumerName object inside checkNullReference is not the same as the ConsumerName object inside AddReference.

它只是简单地创建一个位于 addReference 函数堆栈内的新本地对象。因此,一旦函数返回,您就丢失了该对象。换句话说,checkNullReference 中的ConsumerName 对象与AddReference 中的ConsumerName 对象不同。