0

このコードで NPE を取得しています。各インデックスを初期化しようとしましたが、うまくいかないようです。何が間違っているのか指摘していただけますか?ありがとう!

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

}
4

1 に答える 1

2

NPE のスタック トレースを参照すると便利です。;)

これにより、配列の配列ではなく、配列への参照の配列が作成されます。

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

この意味は

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

table_contents[i] が になるため、NPE をスローする必要がありますnull。デバッガーで調べると、これが確認されます。

最も簡単な解決策は、配列または配列を作成することです

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

ところで、2 つではなく 1 つのループを使用します。

于 2012-06-11T16:53:12.220 に答える