使用动作侦听器的空指针异常错误 [英] null pointer exception error using action listener

查看:132
本文介绍了使用动作侦听器的空指针异常错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在img.setIcon(bg);行上收到错误消息空指针异常;在我的动作监听器中的控制器类中,我不确定为什么。我已在突出显示错误的地方发表了评论。有什么想法吗?

I'm getting the error message null pointer exception on the line img.setIcon(bg); in the controller class in my action listener and I'm not sure why. I've commented where the error is highlighted. Any ideas on why?

    public class Display {
    public ImageIcon bg;
    public JLabel img;
    public void UI() {
        Controller listener = new Controller();
        bg = new ImageIcon("prophase.png");
        img = new JLabel(bg, JLabel.CENTER);
        JFrame gameFrame = new JFrame();
        JPanel top = new JPanel();
        JPanel bottom = new JPanel();
        JPanel imgPane = new JPanel();
        JPanel panel1 = new JPanel();
        imgPane.setLayout(new BorderLayout());
        panel1.setLayout(new BorderLayout());
        panel1.setOpaque(false);// !!
        top.setBorder(BorderFactory.createTitledBorder(""));
        bottom.setBorder(BorderFactory.createTitledBorder(""));
        JLabel jl = new JLabel("What stage of mitosis is this?", JLabel.CENTER);
        imgPane.add(img);// center
        top.add(jl);// top center
        top.add(listener.wordListener());// top center
        bottom.add(listener.answer);// bottom
        panel1.add(imgPane, BorderLayout.CENTER);// background image (center)
        panel1.add(top, BorderLayout.NORTH);// text field and jlabel (top)
        panel1.add(bottom, BorderLayout.SOUTH);// blank spaces and letters used
        gameFrame.setJMenuBar(menuBar());
        gameFrame.setTitle("Mitosis");
        gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gameFrame.setIconImage(new ImageIcon("logo.png").getImage());
        gameFrame.setResizable(false);
        gameFrame.add(panel1);
        gameFrame.setSize(400, 300);
        gameFrame.setLocationRelativeTo(null);
        gameFrame.setVisible(true);
    }
}

另一类:

 public class Controller {
    Display display = new Display();
    private JFrame dialogFrame = new JFrame();
    private ImageIcon logo = new ImageIcon("logo.png");
    public String word;
    public JLabel answer = new JLabel(" ", JLabel.CENTER);
        public JTextField wordListener() {
            JTextField tf = new JTextField(10);
            tf.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {// right click key
                    JTextField tf = (JTextField) e.getSource();
                    word = tf.getText();
                    if (word.equalsIgnoreCase("Prophase")) {
                        answer.setText("Correct!");
                        ImageIcon bg = display.bg;
                        JLabel img = display.img;
                        bg = new ImageIcon("interphase.png");
                        img.setIcon(bg);//error
                        answer.setText(" ");
                    } else {
                        answer.setText("Incorrect, try again.");
                    }
                    tf.setText("");
                }// end actionPerformed method
            });
            return tf;
        }
    }


推荐答案

Display 的默认构造函数不会初始化属性 img ,该属性随后被初始化为 null 默认情况下。显然,该例外是由于您尝试使用 img 而不在任何地方对其进行初始化的事实。您应该为 Display 定义一个默认的构造函数,该构造函数是这样的:

The default constructor of Display doesn't initialize the attribute img, which is then initialized to null by default. The exception is clearly due to the fact that you are trying to use img without initializing it anywhere. You should define a default constructor for Display that does something like this:

public Display(){
    UI();
}

因为在 UI()您正在初始化 img
另外,在使用 img 之前,还必须初始化显示,像这样:

Because in UI() you are initializing img. Also you have to initialize your display before using img, like this:

Display display = new Display();

这篇关于使用动作侦听器的空指针异常错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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