出于某种原因,我无法找出我的代码有什么问题。基本上
- 在本例中,我有一个计数器(总计)初始化为 2
- 每次检查
JCheckbox
时最多增加 6(在本例中为 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)
似乎负责所有编辑的所有错误我正在让控制台在该行显示空指针
最佳答案
您的表似乎包含空值。所以你需要在支票中考虑它。像这样的事情:
if (user.getModel().getValueAt(i, 3) == Boolean.TRUE)
如果我的建议不起作用,请提供 SSCCE这样我们也可以重现您的问题。
关于java - 检索 JTable 内 JCheckbox 的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43656806/