Adam
Adam

Reputation: 10016

Null pointers and private members, basic Java GUI with swing

When I try to build the program below, I get the following error:

Exception in thread "main" java.lang.NullPointerException

    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at miniCADFrame$CanvasPanel.<init>(miniCADFrame.java:71)
    at miniCADFrame.<init>(miniCADFrame.java:17)
    at miniCAD.<init>(miniCAD.java:12)
    at miniCAD.main(miniCAD.java:20)

Clearly I've got some NULL pointer issues, but I'm not sure where they are! The code that I think is the source of the problem is below (if I avoid using the classes in the code below, the program will run without problems). I've added in the line numbers for the problem spots below.

Once finished, the program will allow the user to press a button to draw various shapes.

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class miniCADFrame extends JPanel {

    private CanvasPanel canvas = new CanvasPanel(); //LINE 17
    private ButtonPanel buttons = new ButtonPanel();

    public miniCADFrame() {
         //Constructor, creates the mother panel
         this.setLayout(new BorderLayout());
         this.add (canvas,BorderLayout.CENTER);
    }

    private class ButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent event) {    //Paint the figure associated with the button click
                canvas.add(new FigurePanel(FigurePanel.OVAL), BorderLayout.CENTER);
                canvas.repaint();
            }
    }

    class ButtonPanel extends JPanel {

        private JButton[] Jbuttons = new JButton[11];
        //Constructor
        ButtonPanel() {

            setLayout(new GridLayout(4, 4)); 

            // Create buttons to attach to the buttons panel
            Jbuttons[0] = new JButton("Change Colour");
            Jbuttons[1] = new JButton("Up");
            Jbuttons[2] = new JButton("Text");
            Jbuttons[3] = new JButton("Left");
            Jbuttons[4] = new JButton("Enlarge");
            Jbuttons[5] = new JButton("Right");
            Jbuttons[6] = new JButton("Rectangle");
            Jbuttons[7] = new JButton("Down");
            Jbuttons[8] = new JButton("Circle");
            Jbuttons[9] = new JButton("Save");
            Jbuttons[10] = new JButton("Load");

            //Add the buttons to the buttons panel
            for (int i=0; i<11; i++) {
                Jbuttons[i].addActionListener(new ButtonListener());
                buttons.add(Jbuttons[i]);
            }
    }
    }

class CanvasPanel extends JPanel {

        //Constructor
        CanvasPanel() {
            // Create "canvas" to hold a label for the buttons panel along with the button panel itself
             this.setLayout(new BorderLayout());
             this.add(new JLabel("CONTROL PANEL"),BorderLayout.NORTH);
             this.add(buttons, BorderLayout.WEST); //LINE 71 add the button panel to the canvas panel

             //test
             this.add(new FigurePanel(FigurePanel.RECTANGLE), BorderLayout.CENTER);
        }
}
}

Upvotes: 1

Views: 134

Answers (1)

Leon
Leon

Reputation: 988

I think it's because you instantiate the canvas before the buttons. The buttons variable is used in the constructor of CanvasPanel

Upvotes: 4

Related Questions