英文:
IndexOutOfBoundsException: 0>=0 only when using vector.removeAllElements()
问题
我得到以下的代码:
public void tryM(String...Zeilen) {
DefaultTableModel model = new DefaultTableModel();
table = new JTable();
model.setColumnIdentifiers(Zeilen);
table.setModel(model);
table.setFillsViewportHeight(true);
table.setDefaultEditor(Object.class, null);
try {
Class.forName(driverName);
Connection con = DriverManager.getConnection(url, userName, password);
sql = String.format("SELECT rezepte.id, rezepte.NAME, rezeptkategorien.KATNR, kategorien.BESCHREIBUNG\r\n" +
"FROM rezepte inner JOIN rezeptkategorien ON rezeptkategorien.REZEPTID = rezepte.id\r\n" +
"inner JOIN kategorien ON kategorien.ID = rezeptkategorien.KATNR\r\n" +
"WHERE beschreibung=\"%s\"", textvalue);
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
Array test;
int i = 0;
int j = 1;
Vector<String> vector = new Vector<String>();
while (rs.next()) {
while(j != Zeilen.length + 1) {
vector.add(rs.getString(j));
j++;
System.out.println(vector);
}
model.addRow(vector);
vector.removeAllElements();
System.out.println(vector + " test");
i++;
j = 1;
}
if (i == 0) {
amenak = true;
}
} catch(Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
编辑错误:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.base/java.util.Vector.elementAt(Vector.java:466)
at java.desktop/javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:661)
at java.desktop/javax.swing.JTable.getValueAt(JTable.java:2706)
at java.desktop/javax.swing.JTable.prepareRenderer(JTable.java:5724)
at java.desktop/javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2190)
at java.desktop/javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2092)
at java.desktop/javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1888)
at java.desktop/javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
at java.desktop/javax.swing.JComponent.paintComponent(JComponent.java:797)
at java.desktop/javax.swing.JComponent.paint(JComponent.java:1074)
at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
at java.desktop/javax.swing.JComponent.paint(JComponent.java:1083)
at java.desktop/javax.swing.JViewport.paint(JViewport.java:737)
at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
at java.desktop/javax.swing.JComponent.paint(JComponent.java:1083)
at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
at java.desktop/javax.swing.JComponent.paint(JComponent.java:1083)
at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
at java.desktop/javax.swing.JComponent.paint(JComponent.java:1083)
at java.desktop/javax.swing.JLayeredPane.paint(JLayeredPane.java:586)
at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
at java.desktop/javax.swing.JComponent.paintToOffscreen(JComponent.java:5262)
at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBufferedImpl(RepaintManager.java:1643)
at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1618)
at java.desktop/javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1556)
at java.desktop/javax.swing.RepaintManager.paint(RepaintManager.java:1323)
at java.desktop/javax.swing.JComponent.paint(JComponent.java:1060)
at java.desktop/java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
at java.desktop/sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:75)
at java.desktop/sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:112)
at java.desktop/java.awt.Container.paint(Container.java:2002)
at java.desktop/java.awt.Window.paint(Window.java:3928)
at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:876)
at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:848)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:848)
at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:823)
at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:772)
at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1884)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:316)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
英文:
i got the following code:
public void tryM(String...Zeilen) {
DefaultTableModel model = new DefaultTableModel();
table = new JTable();
model.setColumnIdentifiers(Zeilen);
table.setModel(model);
table.setFillsViewportHeight(true);
table.setDefaultEditor(Object.class, null);
try {
Class.forName(driverName);
Connection con = DriverManager.getConnection(url, userName, password);
sql = String.format("SELECT rezepte.id, rezepte.NAME, rezeptkategorien.KATNR, kategorien.BESCHREIBUNG\r\n" + "FROM rezepte inner JOIN rezeptkategorien ON rezeptkategorien.REZEPTID = rezepte.id\r\n" + "inner JOIN kategorien ON kategorien.ID = rezeptkategorien.KATNR\r\n" + "WHERE beschreibung=\"%s\"", textvalue);
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
Array test;
int i = 0;
int j =1;
Vector<String> vector = new Vector<String>();
while (rs.next()) {
while(j!=Zeilen.length+1) {
vector.add(rs.getString(j));
j++;
System.out.println(vector);
}
model.addRow(vector);
vector.removeAllElements();
System.out.println(vector+" test");
//System.out.println(rs.getString(2));
i++;
j=1;
}
if (i == 0) {
amenak = true;
}
} catch(Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
I am trying to add a variable amount of variables into the Vector "vector", then add it to the jtable, and clear it again to be filled again, according to my Print statements, the vector gets filled up and cleared perfectly fine. As soon its done and no more rows can be added, i get "IndexOutOfBounds 0>=0"
when vector.removeAllElements() is removed, no error, but the result is not the wanted one.
Any idea why? Where is a index being looked for? Im kind of stumped.
Kind regards
edit error:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.base/java.util.Vector.elementAt(Vector.java:466)
at java.desktop/javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:661)
at java.desktop/javax.swing.JTable.getValueAt(JTable.java:2706)
at java.desktop/javax.swing.JTable.prepareRenderer(JTable.java:5724)
at java.desktop/javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2190)
at java.desktop/javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2092)
at java.desktop/javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1888)
at java.desktop/javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
at java.desktop/javax.swing.JComponent.paintComponent(JComponent.java:797)
at java.desktop/javax.swing.JComponent.paint(JComponent.java:1074)
at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
at java.desktop/javax.swing.JComponent.paint(JComponent.java:1083)
at java.desktop/javax.swing.JViewport.paint(JViewport.java:737)
at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
at java.desktop/javax.swing.JComponent.paint(JComponent.java:1083)
at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
at java.desktop/javax.swing.JComponent.paint(JComponent.java:1083)
at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
at java.desktop/javax.swing.JComponent.paint(JComponent.java:1083)
at java.desktop/javax.swing.JLayeredPane.paint(JLayeredPane.java:586)
at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
at java.desktop/javax.swing.JComponent.paintToOffscreen(JComponent.java:5262)
at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBufferedImpl(RepaintManager.java:1643)
at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1618)
at java.desktop/javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1556)
at java.desktop/javax.swing.RepaintManager.paint(RepaintManager.java:1323)
at java.desktop/javax.swing.JComponent.paint(JComponent.java:1060)
at java.desktop/java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
at java.desktop/sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:75)
at java.desktop/sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:112)
at java.desktop/java.awt.Container.paint(Container.java:2002)
at java.desktop/java.awt.Window.paint(Window.java:3928)
at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:876)
at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:848)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:848)
at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:823)
at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:772)
at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1884)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:316)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
答案1
得分: 2
vector.removeAllElements();
不要使用removeAllElements()方法。摆脱那个语句。
TableModel的每一行必须是Vector的新实例。不能重用同一个Vector。
Vector<String> vector = new Vector<String>();
while (rs.next()) {
上述代码需要修改为:
//Vector<String> vector = new Vector<String>();
while (rs.next()) {
Vector<String> vector = new Vector<String>();
这样你就有了Vector的新实例。
英文:
vector.removeAllElements();
Don't use the removeAllElements() method. Get rid of that statement.
Each row of the TableModel must be a new instance of the Vector. You can't reuse the same Vector.
Vector<String> vector = new Vector<String>();
while (rs.next()) {
The above code needs to be changed to:
//Vector<String> vector = new Vector<String>();
while (rs.next()) {
Vector<String> vector = new Vector<String>();
So you have your new instance of the Vector.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论