空指针异常错误,我无法搞清楚?

时间:2011-11-03 20:46:34

标签: java swing

我无法弄清楚如何修复我的空指针。我的持有人(持有人模式类)未定义 - 我不知道如何解决它。我对编程还很陌生。感谢有关如何解决这个问题的任何想法!

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的代码,然后我将它存储在我的drawingPanel,Goose类和BehaviorButtons中,以便它与三者相关联。

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);

2 个答案:

答案 0 :(得分:2)

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

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为空,但是如果你包含完整的堆栈跟踪(打印出的错误让你知道你有一个空指针异常),你可以帮我们回答你的问题。

修改

根据你的NPE,我会把美元押在甜甜圈上,你的持有者会以null的形式传递给构造函数。检查它在调试器中的值或打印出其值以进行验证。

答案 1 :(得分:0)

它可以帮助显式验证构造函数/方法参数,并在出现错误时抛出更具信息性的异常:

if (behavior == null)
    throw new NullPointerException("behavior is null");
if (holder == null)
    throw new NullPointerException("holder is null");

那样:

  1. 如果问题是空参数,您不仅会立即知道这是一个参数问题,而是哪个参数是问题。
  2. 如果您在没有任何添加信息的情况下获得NullPointerException,则可以立即知道该问题不是空参数。
  3. 然后,一旦您知道它们不是null

    ,就可以添加参数的进一步验证
    if (holder.getBehavior() == null)
        throw new IllegalArgumentException("holder's behavior is null");