首页 > 解决方案 > 从我的数据库中获取信息并将它们写入 JTable。错误:java.lang.IndexOutOfBoundsException:索引:2,大小:2

问题描述

我尝试从我的数据库中创建一个“用户”列表并将它们添加到我的 JTable 中。

我收到错误消息:

"java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at Administration.User_Verwalten.show_user(User_Verwalten.java:83)
    at Administration.User_Verwalten.<init>(User_Verwalten.java:189)
    at Administration.User_Verwalten$1.run(User_Verwalten.java:62)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.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)"

我已经检查了 ArrayList,但我没有发现任何错误。我已经阅读了有关此错误消息的线程,其中错误出现在 for 循环中,但这里的 for 循环是正确的。这是代码:

ArrayList <User> list = connect.getAllFromUserAsUser();
            DefaultTableModel model1 = (DefaultTableModel) tableUsers.getModel();
            Object [] row = new Object[7];
            for (int i=0; i<=list.size();i++) {
            row [0] = list.get(i).getID();
            row [1] = list.get(i).getBenutzername();
            row [2] = list.get(i).getPasswort();
            row [3] = list.get(i).getName();
            row [4] = list.get(i).getVorname();
            row [5] = list.get(i).getGeburtsdatum();
            row [6] = list.get(i).isIstWirt();
            model1.addRow(row);
            }



public ArrayList<User> getAllFromUserAsUser() throws SQLException { //prüft ob eingegebene LoginID in DB ist
        ArrayList <User> All = new ArrayList <User>(); 
        try {
            String query = "select ID,Benutzername,Passwort,Name,Vorname,Geburtsdatum,IstWirt from user";
            rs= st.executeQuery(query);
            User user;
            while (rs.next()) {
                user= new User(rs.getInt("ID"), rs.getString("Benutzername"), rs.getString("Passwort"),rs.getString("Name"),
                        rs.getString("Vorname"),rs.getDate("Geburtsdatum"),rs.getBoolean("IstWirt"));

                All.add(user);
                }
        }
        catch(Exception e) {
            System.out.println(e);
        }
        finally {
            if (rs!=null) {rs.close();
            }
        }
        return All;

    }

标签: java

解决方案


我认为问题出在 for 循环条件下i<=list.size();i++) ,如果您有一个大小为 4 的数组,那么您最多需要 4 次迭代,例如 i=0,1,2,3。那是 4 次迭代,所以停止条件是数组的大小 - 1。 for 循环应该是 for (int i=0; i<list.size();i++) {


推荐阅读