Java Arraylist中的空指针异常
即使我进行了空检查,我仍收到空指针异常:Java Arraylist中的空指针异常,java,android,arraylist,Java,Android,Arraylist,即使我进行了空检查,我仍收到空指针异常: @Override public ArrayList<DataCache> getData() { if(contentOf != null) { StoreData data = new StoreData(this); if(data!=null) { ArrayList<
@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;
}
}
}
在添加元素之前,还需要初始化
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));
}
}
ArrayList cacheOf=new ArrayList()//在这里初始化
System.out.println(“ContentOf的大小”+ContentOf.Size())//这将是零。
对于(int i=0;i在添加元素之前,还需要初始化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));
}
}
ArrayList cacheOf=new ArrayList();//在此处初始化
System.out.println(“ContentOf的大小”+ContentOf.Size());//这将是零。
对于(int i=0;i在添加元素之前,还需要初始化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));
}
}
ArrayList cacheOf=new ArrayList();//在此处初始化
System.out.println(“ContentOf的大小”+ContentOf.Size());//这将是零。
对于(int i=0;i在添加元素之前,还需要初始化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));
}
}
ArrayList cacheOf=new ArrayList();//在此处初始化
System.out.println(“ContentOf的大小”+ContentOf.Size());//这将是零。
对于(int i=0;i你读到错误了吗
这里的关键是: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();
System.out.println(“ContentOf的值”+ContentOf.get(i).mFeed);
ArrayList cache=contentOf.get(i).mFeed.getData();
您有未初始化的内存。是否读取了错误
这里的关键是: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();
System.out.println(“ContentOf的值”+ContentOf.get(i).mFeed);
ArrayList cache=contentOf.get(i).mFeed.getData();
您有未初始化的内存。是否读取了错误
这里的关键是: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();
System.out.println(“ContentOf的值”+ContentOf.get(i).mFeed);
ArrayList cache=contentOf.get(i).mFeed.getData();
您有未初始化的内存。是否读取了错误
这里的关键是: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();
System.out.println(“ContentOf的值”+ContentOf.get(i).mFeed);
ArrayList cache=contentOf.get(i).mFeed.getData();
您有未初始化的内存。第198行中的内容?第198行将是cacheOf.add(cache.get(i));空指针异常出现在“cacheOf.add(cache.get(i));”初始化cacheOf like user@ρɐ∑ѕρєK将解决您的问题!第198行中内容的可能重复项?第198行将是cacheOf.add(cache.get(i));空指针异常位于“cacheOf.add(cache.get(i));”初始化cacheOf,就像用户@ρρρK所说的将解决您的问题!第198行中的内容可能重复?第198行将是cacheOf.add(cache.get(i));空指针异常位于“cacheOf.add(cache.get(i))”初始化cacheOf like user@ρцσѕρєK said将解决您的问题!第198行中的内容可能重复?第198行将是cacheOf.add(cache.get(i));空指针异常位于“cacheOf.add(cache.get(i));”初始化cacheOf like user@ρρK said将解决您的问题!即使在初始化后,也可能会出现相同的错误我在初始化后删除cacheOf=null,并得到以下错误:02-03 10:41:53.640:E/AndroidRuntime(9143):致命异常:main 02-03 10:41:53.640:E/AndroidRuntime(9143):java.lang.IndexOutOfBoundsException:无效索引0,大小为0 02-03 10:41:53.640:E/AndroidRuntime(9143):在java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)02-03 10:41:53.640:E/AndroidRuntime(9143):在java.util.ArrayList.get(ArrayList.java:308)我将条件检查更改为cache.size()>我成功了。如果i=0,缓存的大小应该至少为1。即使在初始化之后,我也会收到相同的错误我在初始化后删除了cacheOf=null,并得到以下错误:02-03 10:41:53.640:E/AndroidRuntime(9143):致命异常:main 02-03 10:41:53.640:E/AndroidRuntime(9143):java.lang.IndexOutOfBoundsException:无效索引0,大小为0 02-03 10:41:53.640:E/AndroidRuntime(9143):在java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)02-03 10:41:53.640:E/AndroidRuntime(9143):在java.util.ArrayList.get(ArrayList.java:308)我将条件检查更改为cache.size()>我成功了。如果i=0,缓存的大小应该至少为1。即使在初始化之后,我也会收到相同的错误我在初始化后删除了cacheOf=null,并得到以下错误:02-03 10:41:53.640:E/AndroidRuntime(9143):致命异常:main 02-03 10:41:53.640:E/AndroidRuntime(9143):java.lang.IndexOutOfBoundsException:无效索引0,大小为0 02-03 10:41:53.640:E/AndroidRuntime(9143):在java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)02-03 10:41:53.640:E/AndroidRuntime(9143):在java.util.ArrayList.get(ArrayList.java:308)我将条件检查更改为cache.size()>我成功了。如果i=0,缓存的大小应该至少为1。即使在初始化之后,我也会收到相同的错误我在初始化后删除了cacheOf=null,并得到以下错误:02-03 10:41:53.640:E/AndroidRuntime(9143):致命异常:main 02-03 10:41:53.640:E/AndroidRuntime(9143):java.lang.IndexOutOfBoundsException:无效索引0,大小为0 02-03 10:41:53.640:E/AndroidRuntime(9143):在java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)02-03 10:41:53.640:E/AndroidRuntime(9143):在java.util.ArrayList.get(ArrayList.java:308)我将条件检查更改为cache.size()>如果i=0,缓存的大小应该至少为1。