0

I have limited knowledge with GUI's. I am trying to create a very basic GUI and have the following so far:

public class Controller {

    // variables   
    private JFrame ACMEFrame;
    private JPanel contentPane;
    private JPanel contentPanel;
    private JPanel detailsPanel;
    private JPanel buttonPanel;
    private JLabel label;
    private JComboBox box;

    private JButton ok;
    private JButton quit;


    // constructor
    public Controller() {
        handleAddCruiseToShip();
    }

    public final void handleAddCruiseToShip() {

        ACMEFrame = new JFrame("Assign Cruise to Ship"); // name of frame
        ACMEFrame.setSize(600, 400); // size of JFrame
        ACMEFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // exit JFrame on close

        contentPanel = new JPanel();
        contentPanel.setBackground(Color.GREEN);

        quit = new JButton("Quit");
        ok = new JButton("Ok");

        detailsPanel.add(quit);
        detailsPanel.add(ok);

        ACMEFrame.add(detailsPanel);

        ACMEFrame.pack();
        ACMEFrame.setVisible(true);

    }
}

However, upon running the test class(guiTest), I am coming across the following errors:

Exception in thread "main" java.lang.NullPointerException
    at Controller.AddCruiseToShip(Controller.java:42)
    at Controller.<init>(Controller.java:26)
    at guiTest.main(guiTest.java:10)

Previously, I was able to run the test class and the GUI was displayed, however, now I am coming across errors.

This is my guiTest Class:

public class guiTest {

public static void main (String args []){

    new Controller();
}

}

How do I overcome this?

Thank you.

user3650137
  • 31
  • 1
  • 6

1 Answers1

0

You never instantiate your detailsPanel. Then you add buttons to it and add it to ACMEFrame. That is causing a null pointer exception because you have only declared it. You need to add something like this as you have done with your contentPanel:

detailsPanel = new JPanel();

For that matter it might make sense to do something with your contentPanel since you've created it...

sage88
  • 4,104
  • 4
  • 31
  • 41