jTable不断抛出NullPointerException

时间:2012-02-05 23:04:29

标签: java arrays swing jtable nullpointerexception

我有一个相当混乱的程序设置,允许用户在jDialog中选择表中的行数,输入数据,并将几个数组(对于每一列)的数据返回到主窗体。我觉得昨晚一切顺利,但今天早上无论我做什么,表都会给我一个NullPointerException错误。我已经在网上做了一些研究,并对桌子进行了多次测试,我不知道出了什么问题。 这就是我所拥有的:(另外,第一列中的数据被忽略,这不是错误。我的表有7列。)

这是抛出异常的地方;我正在尝试访问第7列的数据。

public int[] getWDurabilityEV() {
    int rowCount = tbl_Units.getRowCount();
    int[] value = new int[rowCount];
    for (int i=0;i<value.length;i++) {
        value[i] = Integer.parseInt(tbl_Units.getValueAt(i, 6).toString());
    }
    return value;
}

这是主窗体上的按钮单击事件(其他获取与我的示例完全相同,具有不同的列索引;最后的for循环是我如何在主窗体上更新表的示例)

   private void EVButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        dialog_EV1 EV1 = new dialog_EV1(new javax.swing.JFrame(), true);
//setting values
        EV1.ev1_weapon = ev1_weapon;
        EV1.ev1_wstrength = ev1_wstrength;
        EV1.ev1_wrange = ev1_wrange;
        EV1.ev1_wrate = ev1_wrate;
        EV1.ev1_wdurability = ev1_wdurability;
        EV1.ev1_wportability = ev1_wportability;
        EV1.setData();
        EV1.show();
//getting values
        ev1_weapon = EV1.getWeapon();
        ev1_wstrength = EV1.getWStrengthEV();
        ev1_wrange = EV1.getWRangeEV();
        ev1_wrate = EV1.getWRateEV();
        ev1_wdurability = EV1.getWDurabilityEV();
        ev2_wportability = EV1.getWPortabilityEV();
//calcuating values and updating table
        for (int i=0;i<ev1_weapon.length;i++) {
            tbl_Units.setValueAt(ev1_weapon[i],i,1);
        }   
    } 

最后,抛出异常:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at light.dialog_EV1.getWDurabilityEV(dialog_EV1.java:200)
    at light.MainGUI.EVButton1ActionPerformed(MainGUI.java:1061)
    at light.MainGUI.access$100(MainGUI.java:28)
    at light.MainGUI$2.actionPerformed(MainGUI.java:167)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6288)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6053)
    at java.awt.Container.processEvent(Container.java:2041)
    at java.awt.Component.dispatchEventImpl(Component.java:4651)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4481)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.java:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478)
    at java.awt.Component.dispatchEvent(Component.java:4481)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)
    at java.awt.EventQueue.access$000(EventQueue.java:84)
    at java.awt.EventQueue$1.run(EventQueue.java:602)
    at java.awt.EventQueue$1.run(EventQueue.java:600)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:616)
    at java.awt.EventQueue$2.run(EventQueue.java:614)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

1 个答案:

答案 0 :(得分:3)

从您的问题或代码中无需清除,请使用SSCCE

编辑您的问题

1)创建公共AbstractTableModel来存储数据,

2)将TableModel添加到JTable

3)将ListSelectionMode(SINGLE_SELECTION)添加到JTable, 通知测试

if(selectedRow > -1)

4)JTable知道跟随data types,然后没有理由从String e.i解析Integer。