[英]Array List Null Pointer Exception - Android
I am trying to create an application which retrieves the users favourite book quote however I am stuck on how to display this information back to the user. 我试图创建一个可检索用户喜欢的书报价的应用程序,但是我仍然坚持如何将这些信息显示给用户。 I created an ArrayList which will store all the information.
我创建了一个ArrayList来存储所有信息。 However when displaying this information, I keep getting the error:
但是,在显示此信息时,我不断收到错误消息:
java.lang.NullPointerException
java.lang.NullPointerException
when it tries to execute the code 当它尝试执行代码时
temp[i] = new HashMap<String,String>();
This class is shown below: 此类如下所示:
public class FavouriteQuotesActivity extends ListActivity {
static final ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
private void getFavorites() {
DataBaseHelper myDbHelper = new DataBaseHelper(this);
String favorites [] = myDbHelper.getFavourites();
if(list.size() > 0)
{
list.removeAll(list);
}
for(int i = 0;i < favorites.length; i++)
{
String quotes = favorites[i];
String[] quoteArray = quotes.split(":");
HashMap<String,String> temp[] = null;
temp[i] = new HashMap<String,String>();
temp[i].put("Author",(quoteArray[2]));
temp[i].put("Quote",(quoteArray[4]));
list.add(temp[i]);
}
}
Any help would be greatly appreciated. 任何帮助将不胜感激。
Look at this code: 看下面的代码:
HashMap<String,String> temp[] = null;
temp[i] = new HashMap<String,String>();
You've just assigned a null value to the temp
variable (which would more typically have a declaration of HashMap<String, String>[] temp
) - so of course you'll get a NullPointerException
when you then try to access temp[i]
on the very next statement. 您刚刚为
temp
变量分配了一个空值(通常会有一个HashMap<String, String>[] temp
)-因此,当您尝试访问temp[i]
时,您当然会得到NullPointerException
temp[i]
在下一个语句中。
It's not clear why you're using an array at all in that code - why aren't you just using: 目前尚不清楚为什么在该代码中完全使用数组-为什么不仅仅使用:
HashMap<String, String> map = new HashMap<String, String>();
map.put("Author", quoteArray[2]);
map.put("Quote", quoteArray[4]);
list.add(map);
Additionally it's unclear why you're using a map at all here, given that it will always have the same two keys. 另外,由于地图始终具有相同的两个键,因此尚不清楚您为什么在这里使用地图。 Why not create a
Quote
class with name and author properties? 为什么不使用名称和作者属性创建
Quote
类? Also, using a static variable here seems like a bad idea - why doesn't getFavorites
create a new list on each invocation, and return it at the end of the method? 另外,在这里使用静态变量似乎是个坏主意-为什么
getFavorites
不会在每次调用时创建一个新列表,并在方法末尾将其返回?
temp
doesn't point to an array. temp
不指向数组。 You are setting it to null
. 您将其设置为
null
。
You declared the variable temp as an array (by writing HashMap temp[]
). 您将变量temp声明为数组(通过编写
HashMap temp[]
)。
Without being it an actual array, you can't set any elements. 如果不是实际数组,则无法设置任何元素。
You are declaring and using temp
wrong. 您声明并使用
temp
错误。 You don't need an array of HashMap
, just a single HashMap
, since list
is an ArrayList<HashMap>
: 您不需要一个
HashMap
数组,只需一个HashMap
,因为list
是ArrayList<HashMap>
:
HashMap<String,String> temp = new HashMap<String,String>();
temp.put("Author",(quoteArray[2]));
temp.put("Quote",(quoteArray[4]));
list.add(temp);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.