For reasons that even my com sci teacher can't figure out, I am trying to call a method in my main but when i run the program, java throws the following error:
Exception in thread "main" java.lang.NullPointerException at GUIcreatorr.AddComponents(GUImaker5.java:390) at GUImaker5.main(GUImaker5.java:425) Java Result: 1 BUILD SUCCESSFUL (total time: 5 seconds)
here's my code:
`class GUIcreatorr implements ActionListener
{
private JFrame frame;
private CardLayout cardLayout = new CardLayout();
private JPanel contentPanel;
//more constructors here for the various panels i am adding to contentPanel
public void AddComponents()
{
//ADDS ALL FIVE PANELS TO CONTENT PANE
contentPanel.setLayout(cardLayout);
contentPanel.add(panelMenu,"panelMenu");
contentPanel.add(panelMenu,"infoPanel");
contentPanel.add(panel1,"panel1");
contentPanel.add(panel2,"panel2");
contentPanel.add(panel3,"panel3");
contentPanel.add(panel3,"winPanel");
contentPanel.add(panel3,"losePanel");
frame = new JFrame("Maple Gang-Gang: Shooting Cups game");
frame.setContentPane(contentPanel);
cardLayout.show(contentPanel, "panelMenu");
frame.setSize(1000, 800);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
throw new UnsupportedOperationException("Not supported yet.");
}
}
public class GUImaker5
{
public static void main(String[] args)
{
GUIcreatorr letsPlay = new GUIcreatorr();
letsPlay.AddComponents();
}
}`
As you can see, my method AddComponents takes all the panels i created in previous methods of the same class, and adds them to a content pane to be displayed. Am I doing something wrong, or is there a way to simply make java ignore the NullPointerException error? I am a beginner programmer, but I've heard of using try/catch exceptions. should I use one of those to fix my problem,and if so how?
thanks in advance for the desperately needed help!!!