使用动作侦听器的java null指针异常错误

时间:2014-01-02 00:10:51

标签: java swing nullpointerexception actionlistener

我在行img.setIcon(bg)上收到错误消息空指针异常;在我的动作监听器的控制器类中,我不知道为什么。我已经评论了错误突出显示的位置。关于为什么的任何想法?

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

1 个答案:

答案 0 :(得分:2)

您尚未在名为UI()的对象上调用display方法。

这样做的结果是img对象尚未设置display。当您尝试使用img时,这会为您提供空指针异常。