尝试使用通过Iterator检索的对象时获取NullPointerException [英] Getting NullPointerException when trying to use an object retrieved using Iterator
问题描述
这是我的代码,它将元素添加到ArrayList
I this is my code which adds elements to 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));
}
}
以下是迭代器代码
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 am getting null pointer exception at line:
optionselect.setAttribute( "name", country.getShortName() );
PS:由于它在生产环境中运行,因此无法对其进行调试,因此无法在本地服务器上进行复制
PS: I cannot debug it as it is running on production and i cannot replicate it on local server
乍一看,它似乎在ArrayList中有一些null,但是从代码中我无法确定这是怎么可能的。
From the first look it looks like there is some value null in the ArrayList, but from code I cannot make out how it is possible.
有人可以给它一些启发(也许是Java 5的一个错误)。
Can someone shed some light on it(maybe a bug of Java 5).
编辑:我也用此代码得到了空指针
I am also getting null pointer with 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());// Throwing Exception
}
这确认了一件事,在国家/地区列表
中有一个 null
对象,代码我看不到这怎么可能
This confirm one thing that there is somewhere null
object in the countries List
, but looking at the code I cannot see how is that possible
堆栈跟踪如下:
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());
Users.java:985 is 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());
Where IlrnCountriesDTO.java:45 is countryCodes.add(x, c.getCode());
也请注意该国家/地区列表仅加载一次(在首次请求且国家/地区为私有时)
Also please note that countries List is loaded only once(when requested first time and countries is private)
经过分析,我认为这是一个多线程问题。看完ArrayList代码后,我发现它不是同步的
(因此对于相同的索引,大小可能增加了两次)
After analyzing I think this is a multi threading issue. After looking at the ArrayList code I see it is not synchronized
(so maybe size was incremented two time for 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_;
/**
* 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_;
}
推荐答案
您添加的代码看起来有点off ...您有
Your adding code looks a little off... You have
if (countries != null){
countries = new ArrayList();
...
但不应该这样:
if (countries == null){
countries = new ArrayList();
通常,我们初始化是否为空,否则我们会吹走已经存在的任何东西
Normally, we initialise if something is null, otherwise we'd be blowing away anything that was already there.
这篇关于尝试使用通过Iterator检索的对象时获取NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!