0

My code goes like this:

HashMap<Integer, ArrayList<DebtCollectionReport>> mapOfAccounts = new HashMap<Integer, ArrayList<DebtCollectionReport>>();
  Set<String> agencyNames = agencyWiseAccountMap.keySet();
  Iterator iter = agencyNames.iterator();
  while (iter.hasNext()) {
    String agency = (String) iter.next();
    HashMap<Integer, ArrayList<DebtCollectionReport>> tempAccountsMap = agencyWiseAccountMap.get(agency);
    Set<Integer> accountSet = tempAccountsMap.keySet();
    Iterator itr = accountSet.iterator();
    while (itr.hasNext()) {
      mapOfAccounts.put((Integer) itr.next(), tempAccountsMap.get((Integer) itr.next()));
    }
  }

I am getting exception trace:

>

 java.util.NoSuchElementException
    at java.util.HashMap$HashIterator.nextNode(Unknown Source)
    at java.util.HashMap$KeyIterator.next(Unknown Source)
    at com.cerillion.debtcollection.collector.CollectionExecutor.execute(CollectionExecutor.java:56)
    at com.cerillion.debtcollection.collector.CollectionExecutor.main(CollectionExecutor.java:24)
2017-11-14 05:00:43,733 ERROR  CollectionExecutor             [main      ] Exception occurred while executing Debt Collection java.util.NoSuchElementException
java.util.NoSuchElementException
    at java.util.HashMap$HashIterator.nextNode(Unknown Source)
    at java.util.HashMap$KeyIterator.next(Unknown Source)
    at com.cerillion.debtcollection.collector.CollectionExecutor.execute(CollectionExecutor.java:56)
    at com.cerillion.debtcollection.collector.CollectionExecutor.main(CollectionExecutor.java:24)

This is occuring for line:

mapOfAccounts.put((Integer) itr.next(), tempAccountsMap.get((Integer) itr.next()));

What can be the possible reason and how can i resolve it ?

2 Answers 2

1

In the code block below, you called hasNext() once but you have called next() two times. hasNext() will return true if iteration has more values and next() returns the next element in iteration

while (itr.hasNext()) {
  mapOfAccounts.put((Integer) itr.next(), tempAccountsMap.get((Integer) itr.next()));
}

You can change this line accordingly:

while (itr.hasNext()) {
  Integer i1 = (Integer) itr.next();
  if(itr.hasNext()){
    mapOfAccounts.put(i1, tempAccountsMap.get((Integer) itr.next()));
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0
mapOfAccounts.put((Integer) itr.next(), tempAccountsMap.get((Integer) itr.next()));

The problem should be with itr.next(), every time you called itr.next(), the index of the iterator would move a step forward. So the code move 2 steps at this line... You should with a var to accept the value, then use the var:

int accountIdTemp = itr.next();
mapOfAccounts.put((Integer) accountIdTemp , tempAccountsMap.get((Integer) accountIdTemp ));

Hope this help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.