如果我单击Jtable中的空白第8列,我想激活一个按钮。但是我得到这个:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at records$1.valueChanged(records.java:57)


这是我的代码:

tb_records.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent event) {

                int row = tb_records.getSelectedRow();
                DefaultTableModel model = (DefaultTableModel)tb_records.getModel();
                String hehe = (String) model.getValueAt(row, 7);

                if(!hehe.equals("")) {
                    b_extend.setEnabled(false);
                }
                else {
                    b_extend.setEnabled(true);
                }
            }
        });

最佳答案

我对您的代码进行了一些更改,可能会有所帮助。

.(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent e) {

            if (! e.getValueIsAdjusting() ) {
              ListSelectionModel d = (ListSelectionModel)e.getSource();
              if(d.getLeadSelectionIndex() != -1){
                   String hehe = (String) model.getValueAt(d.getLeadSelectionIndex(), 7);
                   b_extend.setEnabled("".equals(hehe));
              }
            }

            }
        });

关于java - 单击JTable中的一行时出现NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20568098/

10-10 16:48