2010-05-27 91 views
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); 
     } 
    } 

} 

上面代码执行我收到 “java.lang.reflect.InvocationTargetException” 混得 COD =(字符串)iter.next()此异常; 行,因为iter.next();返回BigDecimal值,我转换成字符串变量java.lang.reflect.InvocationTargetException在Java中

请帮我

+1

没有想法在哪里?没有堆栈跟踪或行号或任何东西? – 2010-05-27 09:58:37

+1

可能是如何不问问题的一个很好的例子。 你能提供上下文吗?你正在使用哪个库,哪条线发生异常,堆栈跟踪? – 2010-05-27 10:00:17

回答

3

你打电话next()两次,但只有在while循环条件检查hasNext()一次。如果您的列表中包含奇数个元素,则此代码将抛出一个NoSuchElementException,这可能会在某个地方被封装为InvocationTargetException

2

不能直接投一个BigDecimal为String。尝试iter.next()。toString()来代替。

此外,它是使用仿制药的迭代器,因为它使得它更清晰返回什么内容(你可以直接访问类的具体方法(无投需要))是一个好主意。

+0

谢谢,但我还没有实践泛型 – manoj 2010-05-27 10:05:48

相关问题