即使我已经放置了空检查,我也得到空指针异常:
@Override
public ArrayList<DataCache> getData()
{
if(contentOf != null)
{
StoreData data = new StoreData(this);
if(data!=null)
{
ArrayList<DataCache> cacheOf = null;
System.out.println("Size of ContentOf"+contentOf.size());
for (int i=0;i<contentOf.size();i++)
{
System.out.println("Value of ContentOf"+contentOf.get(i).mFeed);
ArrayList<DataCache> cache = contentOf.get(i).mFeed.getData();
if (cache != null)
cacheOf.add(cache.get(i));
}
return cacheOf;
}
}
}
例外:
02-03 10:19:18.770: E/AndroidRuntime(8680): FATAL EXCEPTION: main
02-03 10:19:18.770: E/AndroidRuntime(8680): java.lang.NullPointerException
02-03 10:19:18.770: E/AndroidRuntime(8680): at
com.activity.MainFragmentActivity.getData(MainFragmentActivity.java:198)
答案 0 :(得分:3)
还需要在添加元素之前初始化cacheOf
ArrayList:
ArrayList<DataCache> cacheOf = new ArrayList<DataCache>(); //initialize here
System.out.println("Size of ContentOf"+contentOf.size()); //This will be zero.
for (int i=0;i<contentOf.size();i++) {
//..your code here...
if (cache != null){
if(i<=cache.size())
cacheOf.add(cache.get(i));
}
}
答案 1 :(得分:0)
您是否阅读过该错误?
这里的关键是:com.activity.MainFragmentActivity.getData(MainFragmentActivity.java:198)
什么线是198?
我怀疑它是其中之一:
System.out.println("Value of ContentOf"+contentOf.get(i).mFeed);
ArrayList<DataCache> cache = contentOf.get(i).mFeed.getData();
您的内存未初始化。