简体   繁体   English

jTable不断抛出NullPointerException

[英]jTable keeps throwing NullPointerException

I have a rather confusing setup for a program that lets the user choose the number of rows in a table in a jDialog, input data, and returns the data in several arrays (for each column) to the main form. 对于程序,我有一个相当混乱的设置,该程序允许用户在jDialog中选择表中的行数,输入数据,然后将几个数组(每个列)的数据返回到主窗体。 I thought things were going pretty smoothly up to last night, but this morning the table keeps giving me a NullPointerException error no matter what I do. 我以为直到昨晚一切都进展顺利,但是今天早晨,无论我做什么,该表一直在给我NullPointerException错误。 I've done some research online and tested the table several times, and I don't know what's wrong. 我已经在网上进行了一些研究,并对该表进行了多次测试,但我不知道出了什么问题。 Here's what I have: (Also, the data from the first column is ignored, it's not a mistake. My table has 7 columns.) 这是我所拥有的:(此外,第一列中的数据将被忽略,这不是错误。我的表有7列。)

This is where the exception is being thrown; 这是引发异常的地方。 I'm trying to access the data of column 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;
}

This is the button click event on the main form (the other gets are exactly like my example with a different column index; the for loop at the end is an example of how I'm updating the table on my main form) 这是主窗体上的按钮单击事件(其他获取与我的示例完全一样,但具有不同的列索引;最后的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);
        }   
    } 

And finally, the exception thrown: 最后,抛出异常:

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)

nothing much to clear from your question, nor from code, please edit your question with SSCCE 从您的问题或代码中都没有什么要清除的,请使用SSCCE编辑您的问题

1) create public AbstractTableModel for storing data, 1)创建用于存储数据的公共AbstractTableModel

2) add TableModel to the JTable 2)将TableModel添加到JTable

3) add ListSelectionMode (SINGLE_SELECTION) to the JTable , notice test 3)将ListSelectionMode (SINGLE_SELECTION)添加到JTable ,注意测试

if(selectedRow > -1)

4) JTable knows follows data types , then there no reason for parsing Integer from String ei 4)JTable知道以下数据类型 ,因此没有理由从String ei解析Integer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM