2012-11-16 21 views
0

檢索對象時獲取的NullPointerException我這是我的代碼,添加元素到ArrayList的試圖用用iterator

if(countries==null){ 
    countries = new ArrayList(); 
    for(int x = 0; x < data.getRowCount(); x++) 
    { 
     String shortName = data.getString(x,0); 
     String code = data.getString(x,1); 
     countries.add(x, new Country(code, shortName)); 
    } 
} 

而且下面是迭代器代碼

for(Iterator iter = countries.iterator(); iter.hasNext();) 
{ 
    Country country = (Country)iter.next(); 
    Element optionselect = doc.createElement("countryselect"); 
    optionselect.setAttribute("name", country.getShortName());//Getting NULL Pointer Exception 
    optionselect.setAttribute("value", country.getCode()); 
    countriesElement.appendChild(optionselect); 
} 

現在我得到空指針在線例外:

optionselect.setAttribute("name", country.getShortName()); 

PS:我無法調試它,因爲它在生產上運行時nd我不能在本地服務器上覆制它

從第一次看,它看起來像在ArrayList中有一些值null,但從代碼我不能弄清楚它是如何可能的。

有人可以闡明它(也許是Java 5的一個bug)。

編輯:我也越來越空指針,此代碼

List countryCodes = new ArrayList(); 
for (int x = 0; x < countries.size(); x++) 
{ 
    Country c = (Country) countries.get(x); 
    countryCodes.add(x, c.getCode());// Throwing Exception 
} 

這證實的一件事就是冥冥中有null對象在countries List,但看代碼,我看不出這怎麼可能

的堆棧跟蹤如下:

Stack Trace:java.lang.NullPointerException 
    at com.ilrn.controller.modules.Users.appendCountryList(Users.java:985) 
    at com.ilrn.controller.modules.Users.setupCountries(Users.java:1348) 
    at com.ilrn.controller.modules.Users.gen_add(Users.java:1329) 
    at com.ilrn.controller.modules.Users.generate(Users.java:190) 
    at com.ilrn.controller.ShellModule.generateShell(ShellModule.java:1958) 

Users.java:985是optionselect.setAttribute("name", country.getShortName());

Stack Trace(number 2):java.lang.NullPointerException 
    at com.ilrn.controller.dto.IlrnCountriesDTO.getCountryCodes(IlrnCountriesDTO.java:45) 
    at sun.reflect.GeneratedMethodAccessor471.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:592) 

哪裏IlrnCountriesDTO.java:45是countryCodes.add(x, c.getCode());

同時請注意,國家列表只加載一次(當請求的第一時間,國家爲私人)

分析後,我認爲這是一個多線程問題。看着ArrayList的代碼後,我看到它是不是​​(所以也許尺寸增加兩個時間對同一指數)

 public void add(int index, E element) { 
    if (index > size || index < 0) 
    throw new IndexOutOfBoundsException(
     "Index: "+index+", Size: "+size); 
    ensureCapacity(size+1);  // Increments modCount!! 
    System.arraycopy(elementData, index, elementData, index + 1, 
     size - index); 
    elementData[index] = element; 
    size++; 
} 

這是國家類(snipet)

private String code_; 
private String shortName_; 

/** 
* Constructor sets code and short name. 
* 
* @param code Specifies the code. 
* @param shortName Specifies the short name. 
*/ 
public Country(String code, String shortName) 
{ 
    code_ = code; 
    shortName_ = shortName; 
} 
public String getCode() { 
     return code_; 
} 

public String getShortName() { 
     return shortName_; 
} 
+0

做*不*附加的東西列表,而迭代它!您在遍歷其值時追加到「國家」;這可能會導致奇怪的例外,甚至是無限循環。很難看到這個代碼如何在當前狀態下工作。 – cdhowie

+0

檢查!= null是否泄漏,您可以嘗試進行平等檢查,並在其他部分添加您的代碼 –

+0

@SashiKant以何種方式泄漏? – cdhowie

回答

1

你添加的代碼看起來有點過......你有

if (countries != null){ 
    countries = new ArrayList(); 
    ... 

但它應該是:

if (countries == null){ 
    countries = new ArrayList(); 

通常情況下,我們初始化是否有空,否則我們會吹走已經存在的任何東西。

+0

我也注意到了,但它不能引起NPE OP抱怨。除非他的線路號碼關閉,並且NPE位於'countries.iterator()' –

+0

請查看我的編輯 – Ankur