Use nested static class as ActionListener for the Outer class

Posted by Digvijay Yadav on Stack Overflow See other posts from Stack Overflow or by Digvijay Yadav
Published on 2012-11-11T04:51:18Z Indexed on 2012/11/11 5:00 UTC
Read the original article Hit count: 147

I want to use an nested static class as an actionListener for the enclosing class's GUI elements. I did something like this:

public class OuterClass {

    public static void myImplementation() {
        OuterClass.StartupHandler startupHandler = new OuterClass.StartupHandler();
        exitMenuItem.addActionListener(startupHandler); // error Line
    }

    public static class StartupHandler implements ActionListener {

   @Override
    public void actionPerformed(ActionEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");

        if (e.getSource() == exitMenuItem) {
            System.exit(1);
        } else if (e.getSource() == helpMenuItem) {
            // show help menu
        }
    }
}
}

But when I invoke this code I get the NullPointerException at the //error Line. Is this the right method to do do this or there is something I did am missing?

© Stack Overflow or respective owner

Related posts about java

Related posts about actionlistener