gpt4 book ai didi

java - 使用 2D 对象数组 java 的 NPE

转载 作者:行者123 更新时间:2023-12-02 07:45:06 25 4
gpt4 key购买 nike

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

}

最佳答案

查看 NPE 的堆栈跟踪会很有用。 ;)

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

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

这意味着

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

应该抛出一个 NPE,因为 table_contents[i] 将为 null。查看调试器将证实这一点。

最简单的解决方案是创建一个或多个数组

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

顺便说一句,我会使用一个循环而不是两个。

关于java - 使用 2D 对象数组 java 的 NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10984282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com