2017-04-27 64 views
0

出於某種原因,我找不到我的代碼出了什麼問題。基本上檢索JTable中JCheckbox的狀態

  1. 我在這種情況下
  2. 增量初始化爲2至6一個最大的計數器(總),每次一個JCheckbox被選中(在這種情況下4),並且當過多則拋出一個消息點擊。
  3. 首先,當總共檢測到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)似乎是負責所有的錯誤都是我想提出控制檯的編輯說,在該行

+0

可能重複[什麼是NullPointerException,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-doi-i-fix -it) –

+0

我已更正我的回答 –

回答

1

你的桌子空指針似乎包含空值。所以你需要在你的支票中考慮它。事情是這樣的:

if (user.getModel().getValueAt(i, 3) == Boolean.TRUE) 

如果我的建議不工作,請提供SSCCE,所以我們也可以重現你的問題。

+0

也許你應該推薦一個[MCVE](https://stackoverflow.com/help/mcve)? –

+0

@ Mr.Polywhirl和SSCCE一樣,但鏈接更短:)。 –

+0

@SergiyMedvynskyy這個結構看起來好嗎? – noobProgrammer