2012-06-11 36 views
0

我在這段代碼中得到了一個N​​PE。我試圖初始化每個索引,但似乎也沒有工作。你能指出什麼是錯的嗎?謝謝!使用二維對象數組的NPE java

package com.js.teachEnglish; 

import java.awt.FlowLayout; 
import java.awt.Font; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.BufferedReader; 
import java.io.FileReader; 

import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 

public class Piggy_Panel extends JPanel { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    int number_lines = getLines(Helper.piggybackResource.getPath()); 

    protected JLabel title_Label = new JLabel("Piggyback words"); 

    protected ImageIcon button_image = new ImageIcon(Helper.soundIconResource); 

    protected JButton title_Button = new JButton(button_image); 

    protected JPanel titlePanel = new JPanel(); 

    protected JScrollPane pane = new JScrollPane(this); 

    protected JLabel[] label_group = new JLabel[number_lines]; 
    protected JButton[] button_group = new JButton[number_lines]; 

    protected String[] information = new String[number_lines]; 

    // Table information 
    protected String[] col_titles = new String[] {"",""}; 
    protected Object[][] table_contents = new Object[number_lines][]; 

    protected JTable table; 

    // constructor 
    public Piggy_Panel() { 
     setLayout(new GridLayout(number_lines+1,1)); 

     // scroll pane 
     pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
     pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 

     // getting the information 
     readInformation(); 

     /* 
     * adding the title seperately 
     * and also adding characteristics for the label 
     * */ 

     titlePanel = new JPanel(); 
     titlePanel.setLayout(new FlowLayout()); 

     // adding the characteristics for the label 
     title_Label.setFont(new Font("Arial",Font.PLAIN,36)); 

     titlePanel.add(title_Label); 
     titlePanel.add(title_Button); 

     add(titlePanel); 

     // calling the ini_labels_buttons() to initlialise the buttons and labels 
     ini_labels_buttons(); 

     // fill the object[][] in 
     fill_table(); 

     // initialising table and adding specific characteristics 
     table = new JTable(table_contents, col_titles); 
     table.setFillsViewportHeight(true); 
     table.setShowHorizontalLines(false); 
     table.setShowVerticalLines(false); 

     add(table); 


     // calling the ini_panel_group() to add everything 

     title_Button.addActionListener(new buttonListener()); 


    } 

    // will read the information from the file 
    public String[] readInformation() { 
     try { 
      String line = null; 

      BufferedReader br = new BufferedReader(new FileReader(Helper.piggybackResource.getPath())); 

      int i = 0; 

      while((line = br.readLine()) != null) { 
       information[i] = line; 
       i++; 
      } 

      return information; 

     }catch(Exception ex) { 
      ex.printStackTrace(); 
     } 

     return null; 

    } 

    // will put out the sound 
    // This will output the sound if a button is clicked 
    class buttonListener implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 
      JButton button = (JButton) e.getSource(); 

      String sayDynamicPath = Helper.sayDynamicResource.getPath() + " "; 

      if(button.equals(title_Button)) { 
       try { 
        Runtime.getRuntime().exec(sayDynamicPath + 
          title_Label.getText()); 
       }catch(Exception ex) { 
        JOptionPane.showMessageDialog(null,""+ex,"Unable to produce sound", 
          JOptionPane.ERROR_MESSAGE); 
        ex.printStackTrace(); 
       } 
      } 
     } 
    } 

    // Initialising the JLabels and JButtons and also adding the relevant functions 
    // these include icons and information 
    public void ini_labels_buttons() { 

     // initialising the buttons first 
     for (int i = 0; i < button_group.length; i++) { 
      button_group[i] = new JButton(button_image); 
     } 

     // initialising the labels 
     for (int i = 0; i < label_group.length; i++) { 
      label_group[i] = new JLabel(information[i]); 
     } 

    } 

    public void fill_table() { 
     // fill in the first col 
     for (int i = 0; i < table_contents.length; i++) { 
      table_contents[i][0] = information[i]; // null pointer here 
     } 

     // fill in the second the col 
     for(int j = 0;j < table_contents.length;j++) { 
      table_contents[j][1] = button_group[j]; 
     } 
    } 

    // this method will get the number of lines and then send it back 
    // to the label group 
    public int getLines(String path) { 
     int n = 0; 

     try { 
      BufferedReader br = new BufferedReader(new FileReader(path)); 

      @SuppressWarnings("unused") 
      String line = ""; 

      while((line = br.readLine()) != null) { 
       n++; 
      } 

     }catch(Exception e) { 
      e.printStackTrace(); 
     } 

     return n; 
    } 

} 
+1

張貼您獲得NPE的線路#。如果你有一個數組,你應該認識到分配一個引用數組會初始化爲null,除非將它們設置爲指向一個對象。聽起來像你錯過了一個。調試器應該比在這裏詢問更快。 – duffymo

+0

它是行162.我不知道如何使用調試器。 –

+0

打開你的編輯器,打開行號,然後轉到第162行。你使用的引用之一是null。 – duffymo

回答

2

在NPE的堆棧跟蹤中找位會很有用。 ;)

這將創建一個數組引用數組,而不是數組數組。

protected Object[][] table_contents = new Object[number_lines][]; 

這意味着

table_contents[i][0] = information[i]; 

應拋出NPE因爲table_contents [Ⅰ]將null。在調試器中查看將證實這一點。

最簡單的解決方案是創建一個陣列或多個陣列與

protected Object[][] table_contents = new Object[number_lines][2]; 

順便說一句,我會用一個環路,而不是兩個。

+0

嗨,你如何使用調試器來找到這些類型的東西,你能指點我的教程,你知道嗎?謝謝! –

+1

最簡單的方法是在NullPointerException上添加斷點並在IDE中運行調試(運行旁邊)如何實現這一點取決於您的IDE。 –

+0

好的,非常感謝:D –