I am my code that adds elements to an ArrayList
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)); } }
And the following iterator code
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 ); }
Now I get the null pointer exception on the line:
optionselect.setAttribute( "name", country.getShortName() );
PS: I cannot debug it, because it works in production, and I cannot replicate it on the local server
At first glance it looks like there is some null value in ArrayList, but from the code I canβt figure out how this is possible.
Can someone shed some light on him (maybe a Java 5 bug).
EDIT: I also get a null pointer using this code
List countryCodes = new ArrayList(); for (int x = 0; x < countries.size(); x++) { Country c = (Country) countries.get(x); countryCodes.add(x, c.getCode());
This confirms one thing that the countries List has a null object, but looking at the code, I donβt see how this is possible
The stack trace is as follows:
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)
Where IlrnCountriesDTO.java:45 countryCodes.add(x, c.getCode());
Also note that the list of countries is loaded only once (when requested the first time, and countries are closed)
After analysis, I think this is a multithreaded problem. Looking at the ArrayList code, I see that it is not synchronized (therefore, maybe the size was doubled for the same index)
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++; }
this is the country class (snipet)
private String code_; private String shortName_; public Country(String code, String shortName) { code_ = code; shortName_ = shortName; } public String getCode() { return code_; } public String getShortName() { return shortName_; }