IndexOutOfBoundsException: 0>=0 only when using vector.removeAllElements()

huangapple go评论74阅读模式
英文:

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(&quot;SELECT rezepte.id, rezepte.NAME, rezeptkategorien.KATNR, kategorien.BESCHREIBUNG\r\n&quot; + &quot;FROM rezepte inner JOIN rezeptkategorien ON rezeptkategorien.REZEPTID = rezepte.id\r\n&quot; + &quot;inner JOIN kategorien ON kategorien.ID = rezeptkategorien.KATNR\r\n&quot; + &quot;WHERE beschreibung=\&quot;%s\&quot;&quot;, textvalue);
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
Array test;
int i = 0;
int j =1;
Vector&lt;String&gt; vector = new Vector&lt;String&gt;();
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+&quot; test&quot;);
//System.out.println(rs.getString(2));
i++;
j=1;
}
if (i == 0) {
amenak = true;
}
} catch(Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), &quot;Error&quot;, 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 &quot;AWT-EventQueue-0&quot; java.lang.ArrayIndexOutOfBoundsException: 0 &gt;= 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&lt;String&gt; vector = new Vector&lt;String&gt;();
while (rs.next()) {

The above code needs to be changed to:

    //Vector&lt;String&gt; vector = new Vector&lt;String&gt;();
while (rs.next()) {
Vector&lt;String&gt; vector = new Vector&lt;String&gt;();

So you have your new instance of the Vector.

huangapple
  • 本文由 发表于 2020年10月13日 20:46:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64335576.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定