KnowledgeGeek
KnowledgeGeek

Reputation: 197

Nullpointer error from my classes

I was given this code by my professor and it should run as is. I compile it and get the following error.

java.lang.NullPointerException
    at WholePanel.<init>(WholePanel.java:59)
    at Assignment7.init(Assignment7.java:19)
    at sun.applet.AppletPanel.run(AppletPanel.java:435)
    at java.lang.Thread.run(Thread.java:744)

I have no idea why this is happening. Here are my classes. I compiled and ran the code, got the error, tried commenting some things out and still nothing. I have programmed some previous applets and they run fine.

Assignment7


import javax.swing.*;

public class Assignment7 extends JApplet
{

 public void init()
  {
    // create a WholePanel object and add it to the applet
    WholePanel wholePanel = new WholePanel();
    getContentPane().add(wholePanel);

    //set applet size to 400 X 400
    setSize (400, 400);
  }

}

Whole Panel


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;

public class WholePanel extends JPanel
{
   private Color currentColor;
   private CanvasPanel canvas;
   private JPanel primary, buttonPanel, leftPanel;
   private JButton erase, undo;
   private ArrayList rectList, tempList;
   private JRadioButton[] colorRButtons;
   private Color[] colors;
   private int x1, y1, x2, y2, x3, y3;
   private boolean mouseDragged = false;

   //Constructor to instantiate components
   public WholePanel()
   {
      //default color to draw rectangles is black
      currentColor = Color.black;
      rectList = new ArrayList();

      //create buttons






      //create radio buttons for 5 colors
      //black will be chosen by default
      colorRButtons = new JRadioButton[5];
      colorRButtons[0] = new JRadioButton("black", true);


      //store 5 colors in an array


      //group radio buttons so that when one is selected,
      //others will be unselected.
      ButtonGroup group = new ButtonGroup();
      for (int i=0; i<colorRButtons.length; i++)
        group.add(colorRButtons[i]);

      //add ColorListener to radio buttons
      ColorListener listener = new ColorListener();
      for (int i=0; i<colorRButtons.length; i++)
        colorRButtons[i].addActionListener(listener);

      //primary panel contains all radiobuttons
      primary = new JPanel(new GridLayout(5,1));
      for (int i=0; i<colorRButtons.length; i++)
        primary.add(colorRButtons[i]);


      //canvas panel is where rectangles will be drawn, thus
      //it will be listening to a mouse.
      canvas = new CanvasPanel();
      canvas.setBackground(Color.white);
      canvas.addMouseListener(new PointListener());
      canvas.addMouseMotionListener(new PointListener());

      JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, canvas);

      setLayout(new BorderLayout());
      add(sp);
    }

   //ButtonListener defined actions to take in case "Create",
   //"Undo", or "Erase" is chosed.
   private class ButtonListener implements ActionListener
    {
      public void actionPerformed (ActionEvent event)
      {










      }
   } // end of ButtonListener

   // listener class to set the color chosen by a user using
   // the radio buttons.
   private class ColorListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
         {
            if (event.getSource() == colorRButtons[0])
             currentColor = colors[0];
            else if (event.getSource() == colorRButtons[1])
             currentColor = colors[1];
            else if (event.getSource() == colorRButtons[2])
             currentColor = colors[2];
            else if (event.getSource() == colorRButtons[3])
             currentColor = colors[3];
            else if (event.getSource() == colorRButtons[4])
             currentColor = colors[4];
         }
    }


 //CanvasPanel is the panel where rectangles will be drawn
 private class CanvasPanel extends JPanel
  {
     //this method draws all rectangles specified by a user
     public void paintComponent(Graphics page)
      {
       super.paintComponent(page);

          //draw all rectangles
          for (int i=0; i < rectList.size(); i++)
            {
             // ((Rect) rectList.get(i)).draw(page);
            }

          //draw an outline of the rectangle that is currently being drawn.
          if (mouseDragged == true)
           {
            page.setColor(currentColor);
            //Assume that a user will move a mouse only to left and down from
            //the first point that was pushed.
            page.drawRect(x1, y1, x3-x1, y3-y1);
           }

      }
    } //end of CanvasPanel class

   // listener class that listens to the mouse
   public class PointListener implements MouseListener, MouseMotionListener
    {
     //in case that a user presses using a mouse,
     //record the point where it was pressed.
     public void mousePressed (MouseEvent event)
      {
        //after "create" button is pushed.





      }

     //mouseReleased method takes the point where a mouse is released,
     //using the point and the pressed point to create a rectangle,
     //add it to the ArrayList "rectList", and call paintComponent method.
     public void mouseReleased (MouseEvent event)
      {




      }

     //mouseDragged method takes the point where a mouse is dragged
     //and call paintComponent nethod
     public void mouseDragged(MouseEvent event)
      {



                canvas.repaint();
      }

     public void mouseClicked (MouseEvent event) {}
     public void mouseEntered (MouseEvent event) {}
     public void mouseExited (MouseEvent event) {}
     public void mouseMoved(MouseEvent event) {}

    } // end of PointListener

} // end of Whole Panel Class

Upvotes: 0

Views: 256

Answers (2)

Scary Wombat
Scary Wombat

Reputation: 44824

In this loop

for (int i=0; i<colorRButtons.length; i++)
    colorRButtons[i].addActionListener(listener);

you are accessing an array of colorRButtons, but only the first one

 colorRButtons[0] = new JRadioButton("black", true);

has been created.

Upvotes: 1

Bay73
Bay73

Reputation: 126

It seems you create only one colorRButtons, but trying to use all five.

So in colorRButtons[0] is not null, but all others are null and using of addActionListener for them is impossible.

Upvotes: 3

Related Questions