Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中通过getter获取0值_Java_Jakarta Ee - Fatal编程技术网

在java中通过getter获取0值

在java中通过getter获取0值,java,jakarta-ee,Java,Jakarta Ee,我有一个包中的getter和setter 我正在设置不同包中的类中的值 我试图从这个类中获取值,这个类同样在单独的包中。 但是我得到的是零值,请帮我解决这个问题 package com.company.pojo; public class ExamplePojo { private int x; private int y; private int z; private String a; private String b; private String c; publ

我有一个包中的getter和setter 我正在设置不同包中的类中的值 我试图从这个类中获取值,这个类同样在单独的包中。 但是我得到的是零值,请帮我解决这个问题

    package com.company.pojo;

    public class ExamplePojo {


private int x;
private int y;
private int z;

private String a;
private String b;
private String c;

public int getX() {
    return x;
}
public void setX(int x) {
    this.x = x;
}
public int getY() {
    return y;
}
public void setY(int y) {
    this.y = y;
}
public int getZ() {
    return z;
}
public void setZ(int z) {
    this.z = z;
}
public String getA() {
    return a;
}
public void setA(String a) {
    this.a = a;
}
public String getB() {
    return b;
}
public void setB(String b) {
    this.b = b;
}
public String getC() {
    return c;
}
public void setC(String c) {
    this.c = c;
}
}

我正在设置值的另一个包是

    package com.company.function;

    import com.company.pojo.ExamplePojo;

    public class SetValue {

ExamplePojo EP = null;

public void setValue(){

     EP = new ExamplePojo();

    EP.setX(10);
    EP.setY(20);
    EP.setZ(30);
    EP.setC("Saurabh");
    EP.setA("mahaesh");
    EP.setB("Kanni");
}

}
在第三个包中,我试图通过getter获取值,它返回0

    package com.company.Execute;

    import com.company.function.SetValue;
    import com.company.pojo.ExamplePojo;

    public class Main {



public static void main(String[] args) {

    SetValue St = new SetValue();
    St.setValue();
    ExamplePojo EP = new ExamplePojo();

    System.out.println(EP.getX());
    System.out.println(EP.getY());
    System.out.println(EP.getZ());
    System.out.println(EP.getA());
    System.out.println(EP.getB());
    System.out.println(EP.getC());
}
}

输出为

          0
          0
          0
          null
          null
          null

问题是您有两个独立的对象,
St.EP
EP
。您可以更改一个,然后检查另一个:

SetValue St = new SetValue();       //
St.setValue();                      // this changes St.EP
ExamplePojo EP = new ExamplePojo(); //
System.out.println(EP.getX());      // this examines EP

关于@NPE所说的,您的问题是您正在管理两个不同的对象

这是一个可能的方式来做什么,你正在寻找

修改:

package com.company.function;

import com.company.pojo.ExamplePojo;

public class SetValue {

  public ExamplePojo setValue(){

    ExamplePojo EP = new ExamplePojo();

    EP.setX(10);
    EP.setY(20);
    EP.setZ(30);
    EP.setC("Saurabh");
    EP.setA("mahaesh");
    EP.setB("Kanni");

    return EP;
  }

}
更改:

SetValue St = new SetValue();
ExamplePojo EP = St.setValue();

如果你能给我举一个例子,说明你希望我如何执行它,我会非常感谢你,可能只是用这个例子;和St.setValue();然后我也得到零result@Saurabh检查我的答案,我给了你一个选项,如何根据NPE所说的内容修改代码。