我的课程中出现Nullpointer错误

时间:2014-02-28 07:44:55

标签: java exception

我的教授给了我这个代码,它应该按原样运行。 我编译它并得到以下错误。

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)

我不知道为什么会这样。这是我的课程。 我编译并运行代码,得到错误,尝试评论一些东西,但仍然没有。我编写了一些以前的小程序,它们运行良好。

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);
  }

}

整个小组


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

2 个答案:

答案 0 :(得分:3)

您似乎只创建了一个colorRButtons,但尝试使用全部五个。

所以在colorRButtons [0]中不是null,但是所有其他都是null,并且使用addActionListener是不可能的。

答案 1 :(得分:1)

在这个循环中

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

您正在访问colorRButtons数组,但只访问第一个

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

已创建。