Java 阻止NPE:从另一个类访问私有对象

Java 阻止NPE:从另一个类访问私有对象,java,nullpointerexception,encapsulation,Java,Nullpointerexception,Encapsulation,我在一个类中声明了一个私有变量,我想在另一个类中访问它。但问题是,当我传递对象flappyBird时,它是空的。我需要做什么改变,这样就不需要了 FlappyBird.java:在此处创建的对象 public class FlappyBird implements ActionListener, KeyListener, MouseListener { private static FlappyBird flappyBird; public static void main(

我在一个类中声明了一个私有变量,我想在另一个类中访问它。但问题是,当我传递对象
flappyBird
时,它是空的。我需要做什么改变,这样就不需要了

FlappyBird.java:在此处创建的对象

public class FlappyBird implements ActionListener, KeyListener, MouseListener
{

    private static FlappyBird flappyBird;

    public static void main(String[] args)
        /* CREATE INSTANCE OF FLAPPBIRD() */
    {
        flappyBird = new FlappyBird();
    }

    public static FlappyBird getBird() {
        return flappyBird;
    }

    public static void paint(Graphics phics) {
        ...
    }
    public class GraphicsRenderer extends JPanel
    {
        private static FlappyBird bird = new FlappyBird();

    public void paint(Graphics phics)
    {
        // Generate game graphics by calling paint() in FlappyBird.
        bird.getBird();
        super.paint(phics); 
        bird.paint(phics);
    }
}
graphicsrenderer.java:在此处访问对象

public class FlappyBird implements ActionListener, KeyListener, MouseListener
{

    private static FlappyBird flappyBird;

    public static void main(String[] args)
        /* CREATE INSTANCE OF FLAPPBIRD() */
    {
        flappyBird = new FlappyBird();
    }

    public static FlappyBird getBird() {
        return flappyBird;
    }

    public static void paint(Graphics phics) {
        ...
    }
    public class GraphicsRenderer extends JPanel
    {
        private static FlappyBird bird = new FlappyBird();

    public void paint(Graphics phics)
    {
        // Generate game graphics by calling paint() in FlappyBird.
        bird.getBird();
        super.paint(phics); 
        bird.paint(phics);
    }
}

你的课错得很厉害。没有一个getter,很多部分都没有意义。以下是代码错误的列表:

  • 没有setter,因此该字段将始终为空

  • 出于某种原因,一个用于实例化的字段

  • 您没有从您实现的接口实现方法。我不会在这里解决这个问题,但你自己实现它

  • FlappyBird类没有方法
    paint()
    。我也不会提到这个问题,因为你可以自己做,你不提供任何关于这个方法的细节

以下是一些修复方法:

public class FlappyBird implements ActionListener, KeyListener, MouseListener {

    private static FlappyBird flappyBird;

    public FlappyBird(/* Some attributes to the bird */) {
        /* Field = attribute */
    }

    public static void main(String[] args) {
        flappyBird = new FlappyBird(/* Constructor Args */);
    }

    public FlappyBird getBird() {
        return flappyBird;
    }

    public void setBird(/* You decide the arguments */) {
        /* Field = argument */
    }
}
我添加了一个构造函数,修复了上面的代码,添加了一个setter。构造函数的调用方式如下:

FlappyBird fb = new FlappyBird(arguments);
现在,在调用时,您需要实例化并调用构造函数。然后,您可以访问这些方法。我将
getBird()
返回值作为实例存储在
b
fb
中。您可以扩展此代码

public class GraphicsRenderer extends JPanel {

    public void paint(Graphics phics) {
        FlappyBird fb = new FlappyBird(/*Args*/);
        FlappyBird b = fb.getBird();
        fb.setBird(/*Args*/);
    }
}

您知道没有名为
getBird()
的方法,并且您的
setBird
/其他部分都是非常错误的,不会造成错误sense@AndrewL我的错,我没有打对嗨,我真的很感谢你试图帮助我,你的广泛的答案是。1问题,你说“你没有从你实现的接口实现方法”是什么意思?你实现了ActionListener、KeyListener和MouseListener。我指出,您需要从接口实现方法,如<代码> Actuple Simulink()/代码>等。如果我做了帮助,请考虑接受:^)哦,我明白了。我只是忽略了实现和其他一些东西,因为我不认为这与实现有关。我还没有完全解决NPE问题,但我还是接受你的答案吧,因为它帮了很多忙。谢谢!如果你需要什么,我很乐意帮忙。没问题,这毕竟是一个QnA网站:)