Java表中的NullPointerException选择行

我打算有一个表格,用户可以输入一列(用户应该输入一个数字),每当他输入一个数字时,就会在它旁边的列上扣除,例如:如果我输入2,原始数字是5,那么该列应该有3个。但是我有一个错误。 所以我认为的第一步是获得选定的行。

这是我的完全例外:

java.awt.Container.processEvent(Unknown Source)在java.awt.Component.dispatchEventImpl(Unknown Source)在java.awt.Container.dispatchEventImpl(Unknown Source)在java.awt.Component.dispatchEvent(Unknown Source)在java在java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)上的java.awt.LightweightDispatcher.rejectMouseEvent(Unknown Source)在java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)上的java.awt.LightweightDispatcher.trackMouseEnter(未知源) .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)at java.awt.EventQueue 。访问java.awt.EventQueue上的$ 200(未知源代码)。java.acurity上的java.rmi.AccessController.doPrivileged(Native Method)处的java.awt.EventQueue上的$ 3.run(未知源代码)$ 3.run(未知源代码)。 ProtectionDomain $ 1.doIntersectionPrivilege(未知源)在java.security.P rotectionDomain java.awt.EventQueue上的$ 1.doIntersectionPrivilege(Unknown Source)java.awt.EventQueue上的$ 4.run(Unknown Source)java.security上的java.security.AccessController.doPrivileged(Native Method)处的$ 4.run(Unknown Source) .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。线程中的异常“AWT-EventQueue”中的异常“java.awt.EventDispatchThread.run(Unknown Source)”中的异常-0“java.lang.ArrayIndexOutOfBoundsException:-1在java.util.Vector.elementData(Unknown Source)处于java.util.Vector.elementAt(Unknown Source)处于javax.swing.table.DefaultTableModel.getValueAt(Unknown Source)at app.ui.Inventory.getSelectedQuanti java中的java.awt.AWTEventMulticaster.mouseEntered(Unknown Source)处的app.ui.Inventory $ 2.mouseEntered(Inventory.java:104)(java.awt.AWTEventMulticaster.mouseEntered(Unknown Source)) .awt.Component.processMouseEvent(Unknown Source)at java.awt.Component.processEvent(Unknown Source)at java.awt.Component.processEvent(Unknown Source)at java.awt.Component.processEvent(Unknown Source)在javax.swing.JComponent.processMouseEvent(Unknown Source) .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 java.awt.Window.dispatchEventImpl上的java.awt.Container.dispatchEventImpl(Unknown Source)上的java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)上的java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)上的.trackMouseEnterExit(Unknown Source) (未知源)在java.awt.Component.dispatchEvent(Un 已知源)java.awt.EventQueue上的java.awt.EventQueue.dispatchEventImpl(Unknown Source)java.awt.EventQueue.access上的$ 200(Unknown Source)java.awt.EventQueue上的$ 3.run(Unknown Source) (Unknown Source)at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)at java.awt.EventQueue $ 4。java.security.ProtectionDomain上的java.security.AccessController.doPrivileged(Native Method)在java.awt.EventQueue的java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)上的java.security.AccessController.doPrivileged(Native Method)上运行java.awt.EventQueue $ 4.run(Unknown Source)。在java.awt.EventDispatchThread.punchOneEventForFilters(Unknown Source)在java.awt.EventDispatchThread.pumpEventsForFilter(未知源)在java.awt.EventDispatchThread.pumpEventsForHierarchy(未知源)在java.awt.EventDispatchThread.pumpEvents()未知源)在java.awt.EventDispatch 线程“AWT-EventQueue-0”中的异常java.lang.ArrayIndexOutOfBoundsException:-1处于java.util.Vector.elementData(未知源)at java.awt.EventDispatchThread.run(未知源) java.util.Vector.elementAt(Unknown Source)at app.ui.Inventory.getSelectedQuantity(Inventory.java:363)at app.ui.Inventory $ 2.mouseEntered(javax.swing.table.DefaultTableModel.getValueAt(Unknown Source) javax.swing.JComponent.processMouseEvent上的java.awt.Component.processMouseEvent(Unknown Source)上的java.awt.AWTEventMulticaster.mouseEntered(Unknown Source)(位于java.awt.AWAventMulticaster.mouseEntered(Unknown Source) (Unknown Source)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源)在java.awt.Component.dispatchEvent(Unknown Source)在java.awt.LightweightDispatcher.retargetMouseEven 在java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)处的java.awt.LightweightDispatcher.processMouseEvent(未知源)在java.awt.LightweightDispatcher.trackMouseEnterExit(Unknown Source)处的java.util.Tag(未知源)(位于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)at java.awt.EventQueue.access $ 200(Unknown源)在java.awt.EventQueue $ 3.run(未知源)在java.awt.EventQueue $ 3.run(未知源)在java.security.AccessController.doPrivileged(本地方法)在java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知源)java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知源)位于java.awt.EventQueue $ 4.run(未知源)位于java.awt.EventQueue $ 4.run(未知源)位于java.security.AccessController.doPrivileged (Native Method)在java.security.ProtectionDomain $ 1.doIntersectionPrivil 在java.awt.EventDispatchThread.pushEventsForHierarchy(java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)未知源)java.awt.EventDispatchThread.pumpEvents(未知源)java.awt.EventDispatchThread.pumpEvents(未知源)java.awt.EventDispatchThread.run(未知源)线程“AWT-EventQueue-0”中的异常java .lang.ArrayIndexOutOfBoundsException:-1在java.util.Vector.elementData(未知源)

我的表格没有分页。 它只是平原。 但我改变了行高和字体的颜色。


这一行:

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

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

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

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


的话题

看到这些行:

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();

看到:

  • JTable.convertRowIndexToModel(int viewRowIndex)
  • 链接地址: http://www.djcxy.com/p/73435.html

    上一篇: NullPointerException in Java Table Selecting rows

    下一篇: automatically change org.mysql to org.sqlite