2012-02-05 171 views
1

我有一個程序,讓用戶選擇jDialog中的表中的行數,輸入數據,並返回幾個數組中的數據(對於每列)到主形成。我認爲直到昨天晚上事情進展順利,但今天早上,無論我做什麼,表格都會給我一個NullPointerException錯誤。我在網上做了一些研究,並多次測試了桌子,我不知道什麼是錯的。 這裏是我的:(還有,從第一列的數據被忽略,這不是一個錯誤。我的表有7列。)jTable繼續拋出空指針異常

這是拋出異常的地方;我試圖訪問數據列的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; 
} 

這是主窗體上按鈕點擊事件(其它得到酷似我用不同的列索引例子;在端部線圈是我如何更新表,我的主要形式)

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) 
+0

你可以發佈文件'dialog_EV1.java'的第200行嗎?因爲那是NPE飛行的地方。 – mtsz 2012-02-05 23:12:06

+1

我強烈建議你縮小你的問題 - 這有點過分。 – 2012-02-05 23:17:49

+0

@mtsz:行是:value [i] = Integer.parseInt(tbl_Units.getValueAt(i,6).toString()); – lopzjk12 2012-02-06 01:05:06

回答

3

沒有什麼太多的清楚你的問題,也沒有從代碼,請編輯你與SSCCE

1)R問題用於存儲數據的創建公共AbstractTableModel

2)添加TableModelJTable

3)添加ListSelectionMode(SINGLE_SELECTION)到JTable, 通知測試

if(selectedRow > -1)

4)JTable知道如下data types,然後沒有理由從字符串e.i解析Integer。

+0

嗨,謝謝你的回答,我儘量壓縮我的問題。回覆您的建議: 1)切換到AbstractTableModel有什麼好處?你能澄清嗎? 2)我已經添加了一個表模型jTable。 3)我不確定這將如何幫助;正在拋出異常... 4)感謝您的數據類型;我在表格中設置了數據類型。 – lopzjk12 2012-02-06 01:14:34