出於某種原因,我找不到我的代碼出了什麼問題。基本上檢索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) –
我已更正我的回答 –