2014-04-13 204 views
2

的原因不明,我有以下的代碼,增加了JLabelsJPanel,然後這個JPanelJFrame,但我得到一個NullPointerException,我不能明白爲什麼。我的代碼如下:空指針異常

public void createImage(String view) { 

      String [] viewSplit = view.split("(?!^)"); 

      if(viewSplit.length == 25) { 
       fiveByFiveLook.setLayout(null); 
       fiveByFiveLook.setBackground(Color.WHITE); 
       fiveByFiveLook.setBounds(48, 75, 404, 404); 
       fiveByFiveLook.setVisible(true); 


       ArrayList<String> viewArray = new ArrayList<String>(Arrays.asList(viewSplit)); 



       JLabel fiveByFiveLabels[][] = new JLabel[5][5]; 

       for(int i = 0, k = 0; i <= 4; i++, k = k + 80) { 
        for(int j = 0, l = 0; j <= 4; j++, l = l + 80) { 


         if(viewArray.get(0).equals("X")) { 
          fiveByFiveLabels[i][j].setIcon(new ImageIcon("Blank.jpg")); 
          fiveByFiveLook.add(fiveByFiveLabels[i][j]); 
          viewArray.remove(0); 
         } 
         else if(viewArray.get(0).equals("#")) { 
          fiveByFiveLabels[i][j].setIcon(new ImageIcon("Wall.jpg")); 
          fiveByFiveLook.add(fiveByFiveLabels[i][j]); 
          viewArray.remove(0); 
         } 
         else if(viewArray.get(0).equals(".")) { 
          fiveByFiveLabels[i][j].setIcon(new ImageIcon("Floor.jpg")); 
          fiveByFiveLook.add(fiveByFiveLabels[i][j]); 
          viewArray.remove(0); 
         } 
         else if(viewArray.get(0).equals("E")) { 
          fiveByFiveLabels[i][j].setIcon(new ImageIcon("Exit.jpg")); 
          fiveByFiveLook.add(fiveByFiveLabels[i][j]); 
          viewArray.remove(0); 
         } 
         else if(viewArray.get(0).equals("G")) { 
          fiveByFiveLabels[i][j].setIcon(new ImageIcon("Gold.png")); 
          fiveByFiveLook.add(fiveByFiveLabels[i][j]); 
          viewArray.remove(0); 
         } 
         fiveByFiveLabels[i][j].setBounds(l, k, 85, 85); 
        } 
       } 
       humanGameWindow.add(fiveByFiveLook); 
      } 

      else if(viewSplit.length == 49) { 

       sevenBySevenLook.setLayout(null); 
       sevenBySevenLook.setBackground(Color.WHITE); 
       sevenBySevenLook.setBounds(48, 75, 404, 404); 
       sevenBySevenLook.setVisible(true); 

       JButton sevenBySevenLabels[][] = new JButton[7][7]; 

       for(int i = 0, k = 0; i <= 6; i++, k = k + 57) { 
        for(int j = 0, l = 0; j <= 6; j++, l = l + 57) { 
         sevenBySevenLabels[i][j] = new JButton(); 
         sevenBySevenLabels[i][j].setBounds(l, k, 62, 62); 
         //sevenBySevenLabels[i][j].setIcon(new ImageIcon("Sword copy.jpg")); 
         sevenBySevenLook.add(sevenBySevenLabels[i][j]); 
        } 
       } 

       humanGameWindow.add(sevenBySevenLook); 
      } 
     } 

的錯誤是:

java.lang.NullPointerException 
    at dodGUI.createImage(dodGUI.java:324) 
    at dodGUI.handleResponse(dodGUI.java:61) 
    at dodGUI$1.run(dodGUI.java:79) 
    at java.lang.Thread.run(Thread.java:744) 
Null Pointer2: java.lang.NullPointerException 

並且在該行出現:

fiveByFiveLabels[i][j].setBounds(l, k, 85, 85); 

請可有人建議,爲什麼這是

:)

回答

3

該行

JLabel fiveByFiveLabels[][] = new JLabel[5][5]; 

創建一個可以容納5x5標籤的emtpy數組。但裏面沒有標籤。所有陣列條目都是null

插入該行

for(int i = 0, k = 0; i <= 4; i++, k = k + 80) { 
    for(int j = 0, l = 0; j <= 4; j++, l = l + 80) { 

     fiveByFiveLabels[i][j] = new JLabel(); // <--- This one... 

和閱讀http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html