我无法弄清楚的空指针异常错误

我无法弄清楚的空指针异常错误

本文介绍了我无法弄清楚的空指针异常错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何修复我的空指针.我的持有人(持有人模式类)未定义 - 我不知道如何修复它.我对编程还是很陌生.感谢您提供有关如何解决此问题的任何想法!

I can't figure out how to fix my null pointer. My holder (Holder Pattern class) is undefined - I'm not sure how to fix it. I am still pretty new to programming. Thanks for any ideas on how to go about fixing this!

public Goose (JPanel container, GooseBehavior behavior, BehaviorHolder holder) {
    super(container);
    _holder = holder;
    double rnd = Math.random(); //local variable to create random angle
    //System.out.println(rnd);
    this.setRotation(rnd);//sets the rotation angle to a random angle
    this.setSize(25, 20); //sets size
    this.setFillColor(java.awt.Color.RED); //sets color
    this.setWrapping(true); //sets wrapping to true
    _gooseBehavior = behavior; //stores _gooseehavior
    _gooseBehavior = _holder.getBehavior();
}

public void react() {
    _gooseBehavior.stop(); //tells the current _gooseBehavior to stop
    _holder.getBehavior(); //gets the stored behavior from the holder
    _gooseBehavior = _holder.getBehavior(); //sets the new value to one stored in holder
    _gooseBehavior.setTarget(this); //sets the target on goose
    _gooseBehavior.start(); //starts the behavior

}

堆栈跟踪:

Exception in thread "main" java.lang.NullPointerException
at Animal.Goose.<init>(Bee.java:26)
at Animal.DrawingPanel.<init>(DrawingPanel.java:30)
at Animal.ControlPanel.<init>(ControlPanel.java:27)
at Animal.App.<init>(App.java:28)
at Animal.App.main(App.java:39)

这是我在控制面板(顶级对象)中实例化 BehaviorHolder 的代码,然后我将它存储在我的绘图面板、Goose 类和 BehaviorButtons 中,以便它与这三个相关联.

Here's the code where I instantiate BehaviorHolder in my Control Panel (top level object), and then I just have it stored in my drawingPanel, Goose class, and BehaviorButtons so that its associated with the three.

public ControlPanel() {
    super();
    this.setLayout(new BorderLayout());//sets a new BorderLayout
    _drawingPanel = new DrawingPanel(_holder);
    _moveRandomly = new MoveBehavior();
    _doNothing = new StopBehavior();
    _kingGoose = new FollowBehavior(_kingGoose);
    _holder = new BehaviorHolder(_moveBehavior);

推荐答案

如果没有堆栈跟踪和不完整的代码示例很难说,但是您的代码存在多个潜在问题:

Its hard to say without a stack trace and incomplete code example, but there's multiple potential problems with your code:

public Goose (JPanel container, GooseBehavior behavior, BehaviorHolder holder) {
    super(container);

    // removed some code for clarity

    _gooseBehavior = behavior;                     // <<- You store behaviour here
    _gooseBehavior = _holder.getBehavior();        // <<- then immediately override it
}

public void react() {
    _gooseBehavior.stop();
    _holder.getBehavior();                  // <-- this isn't getting assigned to anything
    _gooseBehavior = _holder.getBehavior(); // A duplicate assignment, previously done in constructor. Are you changing holder's behaviour outside of the display class?

    _gooseBehavior.setTarget(this); //sets the target on goose
    _gooseBehavior.start(); //starts the behavior
}

我猜 _gooseBehaviour 为空,但是如果您包含完整的堆栈跟踪,您可以帮助我们回答您的问题(打印出来的错误让您知道您有一个空指针异常).

I would guess _gooseBehaviour is null, but you can help us answer your question if you include the full stack trace (The error that was printed out that let you know you had a null pointer exception).

编辑

根据您的 NPE,我敢打赌,您的持有人将作为空值传递给构造函数的甜甜圈.在调试器中检查它的值或打印它的值以进行验证.

Based off your NPE I'll bet dollar's to donuts that your holder is getting passed into the constructor as a null. Inspect it's value in your debugger or print out its value to verify.

这篇关于我无法弄清楚的空指针异常错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 22:10