2010-05-27 89 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

相關問題