2013-02-20 165 views
0

当我单击时,我不断收到*行之间的一行空指针异常。我一直在看它约一个小时,似乎无法弄清楚为什么。任何帮助将不胜感激。谢谢。java gui空指针异常

private Color currentColor; 
    private char currentChar; 
    private Letter letter; 
    private JComboBox comboBox; 
    private JPanel controlPanel; 
    private Color[] colorArray = {Color.black,Color.blue,Color.cyan,Color.orange,Color.white,Color.yellow}; 
    private String[] colorStringArray = {"black","blue","cyan","orange","white","yellow"}; 
    private ArrayList<Letter> letterList; 
    private JButton button1; 
    private Canvas canvas; 
    public WholePanel() 
    { 
     comboBox = new JComboBox(colorStringArray); 
     // here we use black to draw a letter 
     currentColor = colorArray[Color.black]; 

     // set the default character to 'A' 
     currentChar = 'A'; 

     button1 = new JButton("Undo"); 


     controlPanel = new JPanel(); 
     controlPanel.add(button1); 
     controlPanel.add(comboBox); 

     canvas = new Canvas(); 
     JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, controlPanel, canvas); 

     canvas.addMouseListener(new PointListener()); 
     add(sp); 

     // make this panel listen to mouse 
     addMouseListener(new PointListener()); 

     setBackground(Color.white); 
     //This method needs to be called for this panel to listen to keys 
     //When panel listens to other things, and go back to listen 
     //to keys, this method needs to be called again. 
     requestFocus(); 
    } 
    private class PointListener implements MouseListener 
    { 
     int x; 
     int y; 
     // when a user clicks the panel (applet), 
     // KeyListener is focused in it. 
     public void mousePressed(MouseEvent event) 
     { 
      x = event.getX(); 
      y = event.getY(); 
      letter = new Letter(x,y,currentChar,colorArray[comboBox.getSelectedIndex()]); 
      //****************************** 
      letterList.add(letter); 
      //****************************** 
      for(Letter letter1 : letterList) 
      { 
       letter1.Draw(getGraphics()); 
      } 
      x=y=0; 
      requestFocus(); 
     } 
    } 
} 
+3

我没有看到任何地方,你已经初始化'letterList' – MadProgrammer 2013-02-20 05:55:16

回答

6

您没有初始化letterList

以下行添加到您的构造函数WholePanel:

letterList = new ArrayList<Letter>(); 
+0

咄,我知道它必须是一些愚蠢这样。谢谢 – Covertpyro 2013-02-20 05:58:24