0
if(locs!=null)
{
    System.out.println("location are not null");
    Iterator ite = locs.iterator();
    DefaultComboItem locObj = null;
    ArrayList locCode = null;
    String cod=null;
    String name=null;
    while(ite.hasNext())
    {
        locCode = (ArrayList) ite.next();
        Iterator iter = locCode.iterator();
        while(iter.hasNext())
        {
            cod=(String)iter.next();
            System.out.println("Code="+cod);
            name=(String)iter.next();
            System.out.println("name="+name);
            locObj = new DefaultComboItem(cod, name);
            colRet.add(locObj);
        }
    }

}

on executing above code i am getting "java.lang.reflect.InvocationTargetException" getting this exception on cod=(String)iter.next(); line, because iter.next(); returns bigDecimal value and i am converting into String variable

Please help me

2
  • 1
    No ideas where? No stack trace or line number or anything? Commented May 27, 2010 at 9:58
  • 1
    Probably a good example on how not to ask a question. Can you provide context? Which libraries are you using, which line did the exception occurred on, stack trace? Commented May 27, 2010 at 10:00

2 Answers 2

3

You're calling next() twice, but only checking hasNext() once in the while loop condition. If your list has an odd number of elements, this code will throw a NoSuchElementException, which may be getting wrapped in an InvocationTargetException somewhere.

Sign up to request clarification or add additional context in comments.

Comments

2

You can't cast a BigDecimal directly to String. Try iter.next().toString() instead.

Also it would be a good idea to use generics on the Iterators since it makes it clearer what they return (and you can access the specific methods of that class directly (no cast needed)).

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.