使用2D对象数组java的NPE

使用2D对象数组java的NPE,java,nullpointerexception,Java,Nullpointerexception,我在这个代码中得到了一个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

我在这个代码中得到了一个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;
    }

}
package com.js.teachEnglish;
导入java.awt.FlowLayout;
导入java.awt.Font;
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.io.BufferedReader;
导入java.io.FileReader;
导入javax.swing.ImageIcon;
导入javax.swing.JButton;
导入javax.swing.JLabel;
导入javax.swing.JOptionPane;
导入javax.swing.JPanel;
导入javax.swing.JScrollPane;
导入javax.swing.JTable;
公共类Piggy_面板扩展JPanel{
/**
* 
*/
私有静态最终长serialVersionUID=1L;
int number_line=getline(Helper.piggybackResource.getPath());
受保护的JLabel title_Label=新JLabel(“背驮单词”);
受保护的ImageIcon按钮\u image=新的ImageIcon(Helper.soundIconResource);
受保护的JButton title_Button=新JButton(按钮图片);
受保护的JPanel titlePanel=新的JPanel();
受保护的JScrollPane=新的JScrollPane(此);
受保护的JLabel[]标签组=新的JLabel[行数];
受保护的按钮[]按钮组=新的按钮[行数];
受保护字符串[]信息=新字符串[行数];
//表信息
受保护字符串[]列标题=新字符串[]{“”“”};
受保护对象[]表内容=新对象[行数][];
保护JTable表;
//建造师
公共小猪小组(){
setLayout(新网格布局(线数+1,1));
//滚动窗格
setHorizontalScrollBarPolicy(JScrollPane.HorizontalScrollBar\uAlways);
设置垂直滚动条策略(JScrollPane.VERTICAL\u滚动条总是);
//获取信息
阅读信息();
/*
*分别添加标题
*以及为标签添加特征
*  */
titlePanel=新的JPanel();
titlePanel.setLayout(新的FlowLayout());
//添加标签的特征
title_Label.setFont(新字体(“Arial”,普通字体,36));
标题面板添加(标题标签);
标题面板添加(标题按钮);
添加(标题板);
//调用ini_labels_buttons()初始化按钮和标签
ini_标签_按钮();
//将对象[]填入
填写表格();
//初始化表格并添加特定特征
表=新的JTable(表内容、列标题);
表.setFillsViewPerthweight(真);
表.设置显示水平线(假);
表1.设置显示(假);
增加(表);
//调用ini_panel_group()添加所有内容
title_Button.addActionListener(新按钮Listener());
}
//将从文件中读取信息
公共字符串[]读取信息(){
试一试{
字符串行=null;
BufferedReader br=new BufferedReader(新文件阅读器(Helper.piggybackResource.getPath());
int i=0;
而((line=br.readLine())!=null){
信息[i]=行;
i++;
}
返回信息;
}捕获(例外情况除外){
例如printStackTrace();
}
返回null;
}
//将发出声音
//如果单击按钮,将输出声音
类buttonListener实现ActionListener{
已执行的公共无效操作(操作事件e){
JButton按钮=(JButton)e.getSource();
字符串sayDynamicPath=Helper.SayDynamicSource.getPath()+“”;
如果(按钮等于(标题按钮)){
试一试{
Runtime.getRuntime().exec(sayDynamicPath+
title_Label.getText());
}捕获(例外情况除外){
showMessageDialog(null,“+ex,”“无法产生声音”,
JOptionPane.ERROR\u消息);
例如printStackTrace();
}
}
}
}
//初始化JLabel和JButtons并添加相关函数
//这些包括图标和信息
公共无效ini_标签_按钮(){
//首先初始化按钮
对于(int i=0;i
在NPE的堆栈跟踪处放血会很有用

这将创建对数组的引用数组,而不是数组数组

protected Object[][] table_contents = new Object[number_lines][];
这意味着

table_contents[i][0] = information[i]; 
应该抛出一个NPE,因为表内容[i]将
null
。查看调试器将确认这一点

最简单的解决方案是创建一个或多个具有

protected Object[][] table_contents = new Object[number_lines][2];
顺便说一句,我会使用一个循环,而不是两个。

张贴获得NPE的行。如果你有一个数组,你应该意识到