2014-02-28 37 views
0

我的教授給了我這段代碼,它應該按原樣運行。 我編譯它,並得到以下錯誤。我的課程中的空指針錯誤

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 
+4

給我們一個這裏。告訴我們WholePanel.java第59行的位置是 –

+0

colorRButtons [i] .addActionListener(listener); – KnowledgeGeek

+0

您是否可以確保不要在代碼中添加或刪除行 - 如果行號與您的堆棧跟蹤一致,將會有所幫助。 WholePanel.java:59,發生異常的地方,現在似乎是一條評論... – Patrick

回答

3

看來你只能創建一個colorRButtons,但嘗試使用所有五個。

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

+0

這讓我可以運行它,謝謝! – KnowledgeGeek

1

在該循環

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

正在訪問colorRButtons的陣列,但是隻有第一個

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

已創建。