Getting a NullPointerException in my Swing Goose class

我是研究僧i 提交于 2019-12-25 03:58:06

问题


I think my _holder and/or _gooseAction is null.... Here are the relevant code/classes where _holder and _gooseAction are instantiated:

Goose.java

public class Goose {
    //Goose class
    private ActionHolder _holder;
    private gooseAction _gooseAction;

    public Goose (JPanel container, GooseAction action, ActionHolder holder) {
        super(container); 
        _holder = holder;
        _gooseAction = _holder.getAction();
        _gooseAction = action;  
    }

    public void react() {
        _gooseAction.stop(); 
        _holder.getAction(); 
        _gooseAction = _holder.getAction(); 
        _gooseAction.setObjective(this); 
        _gooseAction.start();   
    }

ControlPanel.java

public class ControlPanel {
    //top level class
    public ControlPanel() { 
         super();
        _holder = new ActionHolder(_moveAction);
          _drawingPanel = new DrawingPanel(_holder); 
         _gooseAction = new GooseAction();
         _doNothing = new StopBehavior();
         _kingGoose = new FollowBehavior(_kingGoose);
    } 

    private GooseAction _gooseAction;
    private ActionHolder _holder;

    public class ActionHolder {
        private GooseAction _GooseAction;
        public ActionHolder(GooseAction Action) {
            _gooseAction = Action;
        }
        public void setAction(GooseAction GooseAction) {
            _gooseAction = GooseAction;
        }
        public GooseAction getAction() {
            return _gooseAction.copy();
        }
    }

public class ActionButton extends JButton {
    public ActionButton(String string, boolean b, GooseAction action, ActionHolder holder) {
        super(string, b);
        _holder = holder;
        _gooseAction = action;

    }
}

private class ActionListener implements ChangeListener {

    public ActionListener(GooseAction action) { 
    }

    public void stateChanged(ChangeEvent e) {
        _holder.setAction(_gooseAction);
    }
}

回答1:


if(_holder == null) System.out.println("_holder is null");
if(_gooseAction == null) System.out.println("_gooseAction is null");


来源:https://stackoverflow.com/questions/8003049/getting-a-nullpointerexception-in-my-swing-goose-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!