简体   繁体   English

仅使用LaF Nimbus进行Nullpointerexception

[英]Nullpointerexception only with LaF Nimbus

I get a Nullpointerexception at addPropertyChangeListener when I am using Nimbus Look and Feel. 当我使用Nimbus Look and Feel时,我在addPropertyChangeListener处获得了Nullpointerexception When I use the Linux Standard LAF (Metal) everything works just fine. 当我使用Linux Standard LAF(Metal)时,一切正常。

Here is a small Testproject which simulates the problem! 这是一个模拟问题的小型Testproject! I have a Class which extends JPanel and provides to Content to a Frame. 我有一个扩展JPanel并将内容提供给Frame的类。 The Finsih button on the frame will only be enabled when some conditions are met (in this case the button2 is pressed). 只有满足某些条件时才会启用框架上的Finsih按钮(在这种情况下按下按钮2)。

This is the Mainclass: 这是主类:

public class Main extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = -3120562776241721109L;
    private JPanel contentPane;
    private JButton button;
    private PropertyChangeListener changeListener;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {


        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Main frame = new Main();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Main() {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
            System.out.println(UIManager.getLookAndFeel());
        } catch (ClassNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (InstantiationException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IllegalAccessException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (UnsupportedLookAndFeelException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        button = new JButton("BUTTON");
        button.setEnabled(false);
        changeListener = new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equalsIgnoreCase("state")) {
                    button.setEnabled((boolean) evt.getNewValue());
                }
            }
        };
        contentPane.add(button, BorderLayout.SOUTH);
        Panel panel = new Panel();
        contentPane.add(panel, BorderLayout.CENTER);
        panel.addPropertyChangeListener(changeListener);
    }

Here is the Panel Class: 这是Panel Class:

public class Panel extends JPanel {
    /**
     * 
     */
    private static final long serialVersionUID = -5036784576513445229L;
    private PropertyChangeSupport changes;
    private boolean state;
    /**
     * Create the panel.
     */
    public Panel() {

        this.changes = new PropertyChangeSupport(this);
        JButton button = new JButton("BUTTON2");
        add(button);
        state = false;
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                state = !state;
                changes.firePropertyChange("state", null, state);
            }
        });

    }

    @Override
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        changes.addPropertyChangeListener(listener);
    }
}

Like is said before, it works without any problem with Metal LAF 就像之前说的那样,它对Metal LAF没有任何问题

Looks like you override addPropertyChangeListeners and you store the listeners in an List called "changes". 看起来您重写了addPropertyChangeListeners,并将侦听器存储在名为“changes”的List中。 Since the UI is installed before the rest of your constructor is executed, your changes variable has not been initialized. 由于UI是在执行其余构造函数之前安装的,因此尚未初始化您的更改变量。

Add a null-test before adding the listener to your List and instantiate the List if needed (and do the same in your constructor as well). 在将侦听器添加到List之前添加空测试,并在需要时实例化List(并在构造函数中执行相同操作)。 I can't remember if initializing the variable inline could work or not (and I don't have a JDK here to test and tell you that). 我不记得初始化变量inline是否可行(我在这里没有JDK来测试并告诉你)。

Or consider not overriding that method. 或者考虑不要覆盖该方法。 Why did you do that in the first place? 你为什么一开始就这样做? It looks unnecessary (but I can't tell precisely without the code). 看起来没必要(但如果没有代码,我无法准确说出来)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM