0

I'm getting a NullPointerException error at line 77 lblNewLabel.setVisible(false); which is called from line 65 runTest(); in the following code. (This is a dummy project I wrote to simulate a problem I'm having in a larger project). What I'm trying to do is change the attribute of several fields, buttons, etc based on user action at various places in the project. I would like to group all the changes in a separate method that can be called from various other methods. I'm still a Java novice, having come from some Visual Basic and Pascal experience. Seems like what I'm trying to do should be straight forward, but for now, I'm at a loss. Thanks in advance for your suggestions.

package woodruff;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

public class MyTest extends JFrame {

    private JPanel contentPane;
    private JTextField txtHasFocus;
    private JLabel lblNewLabel;

    /**
     * Create the frame.
     */
    public MyTest() {
        initialize();
    }

    private void initialize() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 237, 161);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        final JLabel lblNewLabel = new JLabel("This is a label.");
        lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel.setHorizontalTextPosition(SwingConstants.CENTER);
        lblNewLabel.setBounds(10, 25, 202, 14);
        contentPane.add(lblNewLabel);

        JButton btnShow = new JButton("Show");
        btnShow.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                lblNewLabel.setVisible(true);
            }
        });
        btnShow.setBounds(10, 50, 89, 23);
        contentPane.add(btnShow);

        JButton btnHide = new JButton("Hide");
        btnHide.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                lblNewLabel.setVisible(false);
            }
        });
        btnHide.setBounds(123, 50, 89, 23);
        contentPane.add(btnHide);

        txtHasFocus = new JTextField();
        txtHasFocus.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent arg0) {
                // Following results in NullPointerException error
                // at woodruff.MyTest.runTest(MyTest.java:77)
                runTest();
            }
        });
        txtHasFocus.setHorizontalAlignment(SwingConstants.CENTER);
        txtHasFocus.setText("Has Focus?");
        txtHasFocus.setBounds(67, 92, 86, 20);
        contentPane.add(txtHasFocus);
        txtHasFocus.setColumns(10);
    }

    private void runTest() {
        lblNewLabel.setVisible(false);
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MyTest frame = new MyTest();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

1 Answer 1

2

In the initialize() method, you have created a local variable for JLabel, and hence are not initializing the instance field, as a reason it remains initialized to null, and hence NPE.

final JLabel lblNewLabel = new JLabel("This is a label.");

change the above line to: -

lblNewLabel = new JLabel("This is a label.");
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. That took care of it! Now, on to my larger project.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.