简体   繁体   English

java.lang.IndexOutOfBoundsException:使用ArrayList时索引:0,大小:0

[英]java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 while working with ArrayList

I m trying to store all these elements and im getting error like java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 and I'm not sure what my mistakes are. 我正在尝试存储所有这些元素,并且收到诸如java.lang.IndexOutOfBoundsException: Index: 0, Size: 0错误消息java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 ,但我不确定我的错误是什么。

public List<AddUserInformation> insertData(AddUserDetail user) {
    List<AddUserInformation> inserted = null;
    try{
        List<AddUserInformation> addUserInformation = new ArrayList<AddUserInformation>() ;
        ArrayList accName = new ArrayList();

        for (int i = 0; i < user.getListofaccounts().toArray().length ; i++) {
            accName.add(user.getListofaccounts().get(i).getAccountName());                  
        }

        List<AddUserInformation> addUserInformation1 = new ArrayList<AddUserInformation>();
        for (int i = 0; i< accName.toArray().length; i++) {   
            addUserInformation1.get(i).setUserID(user.getUserId());
            addUserInformation1.get(i).setUsername(user.getUsername());
            addUserInformation1.get(i).setPassword(user.getPassword());
            addUserInformation1.get(i).setEmailid(user.getEmailid()); 
            List<AddUserAccountDetails> adduseraccountdetails = new ArrayList<>();
            addUserInformation1.get(i).setAccountName(user.getListofaccounts().get(i).getAccountName());
            addUserInformation1.get(i).setPermission_id(user.getListofaccounts().get(i).getPermission_id());
        }
        inserted = adduserInformationDao.saveAddUserInfo(addUserInformation1);

    } catch (Exception e) {
        LOGGER.error("Exception in adding of account to a user " + user.getUsername(), e);
    }
    return inserted;
}

The thing is accName contains number of accounts lets say 'n' as list and in the second "for" loop finally to replaces new values and showing last list values. 事情是accName包含帐户数量,让我们说“ n”为列表,最后在第二个“ for”循环中替换新值并显示最后一个列表值。

The statement: 该声明:

List<AddUserInformation> addUserInformation1 = new ArrayList<AddUserInformation>();

creates a new empty ArrayList , so when you write addUserInformation1.get(i) in the for loop, you are trying to access the element at index 0 but the list is still empty (no element at 0 ), so you get the exception. 创建一个新的空ArrayList ,因此当您在for循环中编写addUserInformation1.get(i)时, 您试图访问索引为0的元素,但列表仍然为空 (元素为0 ),因此会出现异常。

You must add something to your list (eg, invoking add(...) method) before trying to get(...) elements. 在尝试get(...)元素之前,必须先向列表中添加一些内容(例如,调用add(...)方法)。

As Robert mentioned, you are trying to access an element that is not exist. 如Robert所述,您正在尝试访问不存在的元素。

Note: The List<AddUserAccountDetails> adduseraccountdetails = new ArrayList<>(); 注意: List<AddUserAccountDetails> adduseraccountdetails = new ArrayList<>(); is never in use .. 从未使用过..

You can create an object of class AddUserInformation then add it to your list like this: 您可以创建类AddUserInformation的对象,然后将其添加到列表中,如下所示:

    public List<AddUserInformation>  insertData(AddUserDetail user) {
       List<AddUserInformation>  inserted = null;
        try{
            List<AddUserInformation> addUserInformation = new ArrayList<AddUserInformation>() ;
            ArrayList accName = new ArrayList();

            for( int i = 0; i < user.getListofaccounts().toArray().length ; i++ ) {
               accName.add(user.getListofaccounts().get(i).getAccountName());   
            }
            List<AddUserInformation> addUserInformation1 = new ArrayList<AddUserInformation>();
            for(int i = 0; i< accName.toArray().length; i++) {   
                AddUserInformation obj = new AddUserInformation ();
                obj.setUserID(user.getUserId());
                obj.setUsername(user.getUsername());
                obj.setPassword(user.getPassword());
                obj.setEmailid(user.getEmailid()); 
                obj.setAccountName(user.getListofaccounts().get(i).getAccountName());
                obj.setPermission_id(user.getListofaccounts().get(i).getPermission_id());
                addUserInformation1.add(obj);
                }
            inserted = adduserInformationDao.saveAddUserInfo(addUserInformation1);
          }catch (Exception e) {
        LOGGER.error("Exception in adding of account to a user " + user.getUsername(), e);
        }
        return inserted;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “线程”main“中的异常 java.lang.IndexOutOfBoundsException: Index: 0, Size: 0” with ArrayList? - “Exception in thread ”main“ java.lang.IndexOutOfBoundsException: Index: 0, Size: 0” with ArrayList? java.lang.IndexOutOfBoundsException:无效的索引2,Arraylist中的大小为2? - java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2 in Arraylist? java.lang.IndexOutOfBoundsException:索引:1,大小:1 - java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 java.lang.IndexOutOfBoundsException:索引:2,大小:0 - java.lang.IndexOutOfBoundsException: Index: 2, Size: 0 java.lang.IndexOutOfBoundsException:索引:1,大小:1 onBindViewHolder - java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 onBindViewHolder java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 in Kotlin - java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 in Kotlin GSEA:java.lang.IndexOutOfBoundsException:索引:0,大小:0 - GSEA : java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 java.lang.IndexOutOfBoundsException:无效的索引5,大小为5 - java.lang.IndexOutOfBoundsException: Invalid index 5, size is 5 例外:java.lang.IndexOutOfBoundsException:索引:0,大小:0 - Exception : java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 java.lang.IndexOutOfBoundsException:索引:5,大小:5 - java.lang.IndexOutOfBoundsException: Index: 5, Size: 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM