简体   繁体   中英

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.

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.

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.

You must add something to your list (eg, invoking add(...) method) before trying to get(...) elements.

As Robert mentioned, you are trying to access an element that is not exist.

Note: The 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:

    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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM