设置属性时出现NullPointErexception错误

时间:2013-02-02 17:50:19

标签: java exception-handling nullpointerexception

我在第77行遇到NullPointerException错误 lblNewLabel.setVisible(false); 在以下代码中从第65行 runTest(); 调用。 (这是我写的一个虚拟项目,用于模拟我在一个更大的项目中遇到的问题)。我要做的是根据项目中不同位置的用户操作更改多个字段,按钮等的属性。我想将所有更改分组到一个可以从各种其他方法调用的单独方法中。我仍然是Java新手,来自一些Visual Basic和Pascal经验。似乎我想要做的应该是直截了当,但就目前而言,我不知所措。提前感谢您的建议。

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 个答案:

答案 0 :(得分:3)

initialize()方法中,您为local创建了JLabel变量,因此未初始化instance field,因为它仍然初始化为{{null 1}},因此NPE

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

将上述行更改为: -

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