I have two classes (Main_Menu and CancelListener). When i called an instance of my CancelListener to use the proper actionPerformed method, i get a NullPointerException.
In my MainMenu.java class, i declare the button like this:
JButton button = new JButton();
button.addActionListener(new CancelListener());
Here is my CancelListener() class.
class CancelListener implements ActionListener {
private Main_Menu menu;
public JPanel mpan;
public CancelListener() {
}
@Override
public void actionPerformed(ActionEvent ae) {
System.out.println("worked");
try {
CardLayout c1 = (CardLayout) (menu.MainPanel.getLayout());
c1.show(menu.MainPanel, "AppPanel");
} catch (Exception e) {
System.out.println("Exception here " + e);
}
}
}
Further Information: MainPanel is a JPanel with a CardLayout structure. On the Cancel Button, i want to show a particular Panel. I wanted to separate my button view class from the button logic? it works when i dont use a separate class.
You need the
menuvariable to be set:In CancelListener class:
Then, when constructing: