出于某种原因,我找不到我的代码出了什么问题。基本上检索JTable中JCheckbox的状态
- 我在这种情况下
- 增量初始化为2至6一个最大的计数器(总),每次一个
JCheckbox
被选中(在这种情况下4),并且当过多则抛出一个消息点击。 - 首先,当总共检测到6个时,它会抛出空指针;但如果选择多于6个复选框,则工作良好,然后在单击多个按钮时将其减少到6。
任何人都可以指导我在哪里搞乱我的头?非常感谢你。
public void panelThree(JTable user, JButton save, JButton logout, JButton exit, int j) {
System.out.println("J in external main is: " + j);
save.addActionListener(new ActionListener() {
boolean arr[] = new boolean[user.getRowCount()];
public void actionPerformed(ActionEvent e) {
int total = j;
System.out.println("Total in listener is: "+total);
System.out.println("No of rows: " + user.getRowCount());
for (int z = 0; z < arr.length; z++) {
arr[z] = false;
}
for (int i = 0; i < user.getRowCount(); i++) {
if (Boolean.valueOf(user.getModel().getValueAt(i, 3).toString()) == true) {
if (arr[i] == false) {
arr[i] = true;
total++;
}
} else {
if (arr[i] == true) {
arr[i] = false;
total--;
}
}
}
System.out.println("Total is: " + total);
if (total > 6) {
JOptionPane.showMessageDialog(null, "Please choose up to a maximum of " + (6 - j) + " modules");
} else {
int reply = JOptionPane.showConfirmDialog(null, "Are you sure to enroll in these modules?", "Yes", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null, "Enrollment Successful");
}
}
}
});
}
线if (Boolean.valueOf(user.getModel().getValueAt(i, 3).toString()) == true)
似乎是负责所有的错误都是我想提出控制台的编辑说,在该行
可能重复[什么是NullPointerException,以及如何解决它?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-doi-i-fix -it) –
我已更正我的回答 –