简体   繁体   中英

Nullpointerexception only with LaF Nimbus

I get a Nullpointerexception at addPropertyChangeListener when I am using Nimbus Look and Feel. When I use the Linux Standard LAF (Metal) everything works just fine.

Here is a small Testproject which simulates the problem! I have a Class which extends JPanel and provides to Content to a Frame. The Finsih button on the frame will only be enabled when some conditions are met (in this case the button2 is pressed).

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:

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

Looks like you override addPropertyChangeListeners and you store the listeners in an List called "changes". Since the UI is installed before the rest of your constructor is executed, your changes variable has not been initialized.

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). 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).

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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