在我的 Swing Goose 类中获取 NullPointerException [英] Getting a NullPointerException in my Swing Goose class

查看:48
本文介绍了在我的 Swing Goose 类中获取 NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我的 _holder 和/或 _gooseActionnull.... 以下是 _holder 的相关代码/类_gooseAction 被实例化:

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

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

控制面板.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);
    }
}

推荐答案

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

这篇关于在我的 Swing Goose 类中获取 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆