我正在尝试更改 defaultTableModel 中的 Object[][] 数据中的一个值,但在if (data[i][j] == userFolderName)
尝试将变量更改为“Kathy”时出现空指针异常,以防万一它没有正确读取用户名但它仍然抛出例外。你能看看我的代码,看看我哪里出错了吗?
public class Statistics extends JPanel {
public Object[][] data;
public DefaultTableModel model;
public Statistics() {
super(new GridLayout(1,0));
String[] columnNames = {"Name", "Games Played", "Games Won"};
Object[][] data = {
{"Kathy", new Integer(5), new Integer(2)},
{"Steve", new Integer(2), new Integer(0)},
};
model = new DefaultTableModel(data, columnNames);
JTable table = new JTable(model);
table.setFillsViewportHeight(true);
table.setVisible(true);
table.setEnabled(false);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}
public void addRow(Object[] objects) {
model.addRow(objects);
}
public void updateGamesPlayed(String userFolderName, int gamesPlayed) {
int rowCount = model.getRowCount();
int columnCount = model.getColumnCount();
for (int i = 0; i < rowCount; i++){
for(int j = 0; j < columnCount; j++){
if (data[i][j] == userFolderName){
model.setValueAt(gamesPlayed, i, j+1);
}
}
}
}
}