开发者

java.lang.reflect.InvocationTargetException in Java

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);开发者_Python百科
            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


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.


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)).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜