gpt4 book ai didi

java - 从我的数据库获取信息并将它们写入 JTable 中。错误 : java. lang.IndexOutOfBoundsException : Index: 2, 大小:2

转载 作者:行者123 更新时间:2023-12-01 19:58:01 25 4
gpt4 key购买 nike

我尝试从数据库创建“用户”列表并将它们添加到我的 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;

}

最佳答案

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

关于java - 从我的数据库获取信息并将它们写入 JTable 中。错误 : java. lang.IndexOutOfBoundsException : Index: 2, 大小:2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59023496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com