Java表中的NullPointerException选择行

时间:2014-02-11 06:12:36

标签: java swing nullpointerexception

我计划有一个表格,其中有一个用户将要输入的列(用户应该输入一个数字),每当他输入一个数字时,它将被扣除在它旁边的列上,例如:如果我输入2,原始数字是5,那么该列应该有3.但是我有一个错误。所以我想到的第一步是获取所选行。:

这是我的全部例外:

  

at java.awt.Container.processEvent(Unknown Source)       at java.awt.Component.dispatchEventImpl(Unknown Source)       at java.awt.Container.dispatchEventImpl(Unknown Source)       at java.awt.Component.dispatchEvent(Unknown Source)       at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)       at java.awt.LightweightDispatcher.trackMouseEnterExit(Unknown Source)       at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)       at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)       at java.awt.Container.dispatchEventImpl(Unknown Source)       at java.awt.Window.dispatchEventImpl(Unknown Source)       at java.awt.Component.dispatchEvent(Unknown Source)       at java.awt.EventQueue.dispatchEventImpl(Unknown Source)       在java.awt.EventQueue.access $ 200(未知来源)       在java.awt.EventQueue $ 3.run(未知来源)       在java.awt.EventQueue $ 3.run(未知来源)       at java.security.AccessController.doPrivileged(Native Method)       at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)       at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)       在java.awt.EventQueue $ 4.run(未知来源)       在java.awt.EventQueue $ 4.run(未知来源)       at java.security.AccessController.doPrivileged(Native Method)       at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)       at java.awt.EventQueue.dispatchEvent(Unknown Source)       at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)       at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)       at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)       at java.awt.EventDispatchThread.pumpEvents(Unknown Source)       at java.awt.EventDispatchThread.pumpEvents(Unknown Source)       at java.awt.EventDispatchThread.run(Unknown Source)   线程中的异常" AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException:-1       at java.util.Vector.elementData(Unknown Source)       at java.util.Vector.elementAt(Unknown Source)       在javax.swing.table.DefaultTableModel.getValueAt(未知来源)       at app.ui.Inventory.getSelectedQuantity(Inventory.java:363)       at app.ui.Inventory $ 2.mouseEntered(Inventory.java:104)       at java.awt.AWTEventMulticaster.mouseEntered(Unknown Source)       at java.awt.AWTEventMulticaster.mouseEntered(Unknown Source)       at java.awt.Component.processMouseEvent(Unknown Source)       在javax.swing.JComponent.processMouseEvent(未知来源)       at java.awt.Component.processEvent(Unknown Source)       at java.awt.Container.processEvent(Unknown Source)       at java.awt.Component.dispatchEventImpl(Unknown Source)       at java.awt.Container.dispatchEventImpl(Unknown Source)       at java.awt.Component.dispatchEvent(Unknown Source)       at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)       at java.awt.LightweightDispatcher.trackMouseEnterExit(Unknown Source)       at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)       at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)       at java.awt.Container.dispatchEventImpl(Unknown Source)       at java.awt.Window.dispatchEventImpl(Unknown Source)       at java.awt.Component.dispatchEvent(Unknown Source)       at java.awt.EventQueue.dispatchEventImpl(Unknown Source)       在java.awt.EventQueue.access $ 200(未知来源)       在java.awt.EventQueue $ 3.run(未知来源)       在java.awt.EventQueue $ 3.run(未知来源)       at java.security.AccessController.doPrivileged(Native Method)       at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)       at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)       在java.awt.EventQueue $ 4.run(未知来源)       在java.awt.EventQueue $ 4.run(未知来源)       at java.security.AccessController.doPrivileged(Native Method)       at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)       at java.awt.EventQueue.dispatchEvent(Unknown Source)       at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)       at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)       at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)       at java.awt.EventDispatchThread.pumpEvents(Unknown Source)       at java.awt.EventDispatchThread.pumpEvents(Unknown Source)       at java.awt.EventDispatchThread.run(Unknown Source)   线程中的异常" AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException:-1       at java.util.Vector.elementData(Unknown Source)       at java.util.Vector.elementAt(Unknown Source)       在javax.swing.table.DefaultTableModel.getValueAt(未知来源)       at app.ui.Inventory.getSelectedQuantity(Inventory.java:363)       at app.ui.Inventory $ 2.mouseEntered(Inventory.java:104)       at java.awt.AWTEventMulticaster.mouseEntered(Unknown Source)       at java.awt.AWTEventMulticaster.mouseEntered(Unknown Source)       at java.awt.Component.processMouseEvent(Unknown Source)       在javax.swing.JComponent.processMouseEvent(未知来源)       at java.awt.Component.processEvent(Unknown Source)       at java.awt.Container.processEvent(Unknown Source)       at java.awt.Component.dispatchEventImpl(Unknown Source)       at java.awt.Container.dispatchEventImpl(Unknown Source)       at java.awt.Component.dispatchEvent(Unknown Source)       at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)       at java.awt.LightweightDispatcher.trackMouseEnterExit(Unknown Source)       at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)       at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)       at java.awt.Container.dispatchEventImpl(Unknown Source)       at java.awt.Window.dispatchEventImpl(Unknown Source)       at java.awt.Component.dispatchEvent(Unknown Source)       at java.awt.EventQueue.dispatchEventImpl(Unknown Source)       在java.awt.EventQueue.access $ 200(未知来源)       在java.awt.EventQueue $ 3.run(未知来源)       在java.awt.EventQueue $ 3.run(未知来源)       at java.security.AccessController.doPrivileged(Native Method)       at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)       at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)       在java.awt.EventQueue $ 4.run(未知来源)       在java.awt.EventQueue $ 4.run(未知来源)       at java.security.AccessController.doPrivileged(Native Method)       at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)       at java.awt.EventQueue.dispatchEvent(Unknown Source)       at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)       at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)       at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)       at java.awt.EventDispatchThread.pumpEvents(Unknown Source)       at java.awt.EventDispatchThread.pumpEvents(Unknown Source)       at java.awt.EventDispatchThread.run(Unknown Source)   线程中的异常" AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException:-1       在java.util.Vector.elementData(未知来源)

我的桌子没有分类。它只是平原。但是我改变了行的高度和字体的颜色。

1 个答案:

答案 0 :(得分:0)

这一行:

java.lang.ArrayIndexOutOfBoundsException: -1 at java.util.Vector.elementData(Unknown Source)

建议在调用getQuantity()方法时没有选定的行。因此,tblInventory.getSelectedRow()将返回-1,在此行中生成ArrayOutOfBoundsException

String ballID  = dtm.getValueAt(tblInventory.getSelectedRow(),0).toString();

确保在您拨打电话时选择了一行。


OF-主题

参见以下几行:

DefaultTableModel dtm = (DefaultTableModel) tblInventory.getModel();
String ballID  = dtm.getValueAt(tblInventory.getSelectedRow(),0).toString();

此处tblInventory.getSelectedRow() 视图 中选定的行号,而不是 模型 。如果您的表已排序,那么这些索引将不同,从而导致意外结果。您有两种选择:

向表格请求值

//DefaultTableModel dtm = (DefaultTableModel) tblInventory.getModel();
String ballID  = tblInventory.getValueAt(tblInventory.getSelectedRow(),0).toString();

在各自的模型索引中转换视图索引

int row = tblInventory.getSelectedRow();
int modelIndex = tblInventory.convertRowIndexToModel(row);
DefaultTableModel dtm = (DefaultTableModel) tblInventory.getModel();
String ballID  = dtm.getValueAt(modelIndex,0).toString();

请参阅: