JTable 中的错误不起作用
Posted
技术标签:
【中文标题】JTable 中的错误不起作用【英文标题】:Error in JTable not working 【发布时间】:2014-07-13 13:07:10 【问题描述】:我正在尝试从表中提取一些数据,然后将它们添加到 JTable 中,但它给了我一个例外。 请检查是否有问题 -
final String[] columns = "Messages","Status";
final Object[][] data = new Object[100][];
try
PreparedStatement p = c.prepareStatement("select Message,status from mesaages12 where Sender = ?");
p.setInt(1,empid);
r8 = p.executeQuery();
int k = 0;
while(r8.next())
data[k] = new String[1000];
data[k][0] = r8.getString(1);
if(String.valueAt(r8.getInt()).equals(null))
data[k][1] = "0"
else
data[k][1] = String.valueOf(r8.getInt(2));
k++;
p.close();
catch(Exception e)
e.printStackTrace();
final JTable table = new JTable(data,columns);
table.setPreferredScrollableViewportSize(new Dimension(500,50));
table.setFillsViewportHeight(true);
JScrollPane scroll = new JScrollPane(table);
scroll.setBounds(100,250,500,300);
f9.add(scroll);
我不知道我做错了什么。
表的结构是 -
Message status Sender
text number number
请告诉我我犯了什么错误?
java.sql.SQLException: No data found
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7145)
at sun.jdbc.odbc.JdbcOdbc.SQLGetDataInteger(JdbcOdbc.java:3818)
at sun.jdbc.odbc.JdbcOdbcResultSet.getDataInteger(JdbcOdbcResultSet.jav
:5637)
at sun.jdbc.odbc.JdbcOdbcResultSet.getInt(JdbcOdbcResultSet.java:582)
at ntpc.Sms$1$5.actionPerformed(Sms.java:997)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2
18)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.ja
a:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonMode
.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:25
)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButton
istener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:483
)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionD
main.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionD
main.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionD
main.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThr
ad.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread
java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThr
ad.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.JTable$1.getValueAt(JTable.java:690)
at javax.swing.JTable.getValueAt(JTable.java:2717)
at javax.swing.JTable.prepareRenderer(JTable.java:5719)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2114
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:201
)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1812)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
at javax.swing.JComponent.paintComponent(JComponent.java:778)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JViewport.paint(JViewport.java:731)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5226)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintM
nager.java:1529)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:14
2)
at javax.swing.RepaintManager.paint(RepaintManager.java:1249)
at javax.swing.JComponent.paint(JComponent.java:1040)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java
78)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:1
5)
at java.awt.Container.paint(Container.java:1967)
at java.awt.Window.paint(Window.java:3877)
at javax.swing.RepaintManager$3.run(RepaintManager.java:819)
at javax.swing.RepaintManager$3.run(RepaintManager.java:796)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionD
main.java:76)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:769
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:
18)
at javax.swing.RepaintManager.access$1100(RepaintManager.java:62)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.jav
:1677)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionD
main.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThr
ad.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread
java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThr
ad.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.JTable$1.getValueAt(JTable.java:690)
at javax.swing.JTable.getValueAt(JTable.java:2717)
at javax.swing.JTable.prepareRenderer(JTable.java:5719)
【问题讨论】:
你能和我们分享一下这个例外吗? 是java NULL指针异常 堆栈跟踪指出导致异常的确切行号。你可以检查一下这条线。 分享完整的代码以及堆栈跟踪和确切的行号。 它包含我手动检查的数据 【参考方案1】:我做错了什么?
多次调用r8.next()
可能会导致问题。
从while
循环的外部和内部删除if(r8.next())
。
如果事先不知道大小,请使用List
而不是静态数组。
示例代码:
final String[] columns = "Messages", "Status" ;
final List<Object[]> data = new ArrayList<Object[]>();
...
ResultSet r8 = p.executeQuery();
while (r8.next())
Object[] array = new Object[2];
...
data.add(array);
...
final JTable table = new JTable(data.toArray(new Object[][] ), columns);
JScrollPane scroll = new JScrollPane(table);
编辑
问题出在下面的行,如果状态为null
String.valueOf(r8.getInt(2));
只需使用r8.getString(2)
而不是String.valueOf(r8.getInt(2))
【讨论】:
JTable 有问题我将 while(r8.next()) 删除到 while(true) 我不是说要把它改成while(true)
只是删除额外的 if
检查下一条记录。
我们总是被要求提供 SSCCE/MCVE 的主要原因 :-)以上是关于JTable 中的错误不起作用的主要内容,如果未能解决你的问题,请参考以下文章
Java - 方法更改 JTable 中变量的值(刷新不起作用)
JProgressBar setValue 不起作用,也尝试使用 SwingUtilities
带有按钮 setCellRenderer() 的可执行 Jar 不起作用