Note that there are some explanatory texts on larger screens.

plurals
  1. PONull pointers and private members, basic Java GUI with swing
    text
    copied!<p>When I try to build the program below, I get the following error:</p> <pre><code>Exception in thread "main" java.lang.NullPointerException at java.awt.Container.addImpl(Unknown Source) at java.awt.Container.add(Unknown Source) at miniCADFrame$CanvasPanel.&lt;init&gt;(miniCADFrame.java:71) at miniCADFrame.&lt;init&gt;(miniCADFrame.java:17) at miniCAD.&lt;init&gt;(miniCAD.java:12) at miniCAD.main(miniCAD.java:20) </code></pre> <p>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.</p> <p>Once finished, the program will allow the user to press a button to draw various shapes.</p> <pre><code>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&lt;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); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload