0

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!!!

  • when do you initialise your `contentPanel`? – Stefan Lindner Jan 20 '17 at 14:05
  • 1
    `contentPanel` is null. Your teacher couldn't spot that? – khelwood Jan 20 '17 at 14:06
  • 1
    Just read the "duplicated" answer. It will explain in great detail how you solve such problems. And side note: method names go camelCase in java; they do not start Uppercase. – GhostCat Jan 20 '17 at 14:07
  • 2
    As an extra comment: you talked about try catches, these are not used for this kind of functionality. It will only catch the nullpointer, but your program will still not do what you ask or expect from it. – nick wouters Jan 20 '17 at 14:10
  • @StefanLindner Oh my actual goodness I never noticed that i never initialize contentPanel!! you are absolute life savers, thank you so much :) – helpI'mABeginner Jan 21 '17 at 01:23
  • @khelwood Oh my actual goodness I never noticed that i never initialize contentPanel!! you are absolute life savers, thank you so much :) – helpI'mABeginner Jan 21 '17 at 01:24

0 Answers0