null pointer exception error using action listener

2019-09-21 02:30发布

问题:

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

another class:

 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:

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

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

Display display = new Display();


回答2:

Please check if the path for your image is correct

bg = new ImageIcon("interphase.png");

If a resource is not found you will get a NullPointerException when you try to use that imageicon somewhere else.

Try something along the lines

getClass().getResource("interphase.png");

prepend the directory name if needed, in order to get to your image in your resources.

Also, make sure you are initialising the variable in the Display class before using it.