0

我在以下代码的lblNewLabel.setVisible(false); 第 65 行调用了第 77 行的 NullPointerException 错误。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();
                }
            }
        });
    }
}
4

1 回答 1

2

在该initialize()方法中,您为 创建了一个local变量JLabel,因此没有初始化instance field,这是它保持初始化为 的原因null,因此是NPE

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

将上面的行更改为: -

lblNewLabel = new JLabel("This is a label.");
于 2013-02-02T17:51:47.243 回答