Use nested static class as ActionListener for the Outer class
- by Digvijay Yadav
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?