I have a problem while I set get an element and set into customized value type. Here is my customized type class User
public class User {
private List<Integer> FriendList=new ArrayList<Integer>(); //Uid list
private HashMap<String,String> CheckinMap =new HashMap<String, String>(); // location and time
private int Uid;
public User(int uid){
this.Uid=uid;
}
public int getId(){
return Uid;
}
public void setCheckins(HashMap<String,String> InMap){
this.CheckinMap=(HashMap<String, String>) InMap;
}
public void setAFriend(int uid){
this.FriendList.add(uid);
}
}
Then, I parse a JSON array to get data, I am sure that parsing JSON is fine here.
HashMap<Integer,User> UserMap =new HashMap();
HashMap<String,String> InMap =new HashMap();
for(i=0;i<users.size();i++){
JSONObject auser=(JSONObject)users.get(i);
temp_uid=Integer.parseInt(auser.get("uid").toString());
UserMap.put(temp_uid, new User(temp_uid));
JSONArray checkins=(JSONArray)auser.get("check-ins");
for(j=0;j<checkins.size();j++){
InMap.put( ((ArrayList) checkins.get(j)).get(0).toString(), ((ArrayList) checkins.get(j)).get(1).toString());
}
UserMap.get(i).setCheckin(InMap);
checkin_cnt=checkin_cnt+checkins.size();
}
My Eclipse told me that an exception java.lang.NullPointerException
is at the line. UserMap.get(i).setCheckin(InMap);
Can I only initialize Uid , but set FriendList and CheckinMap later? This sounds wired. Please give me some hints or suggestions. Thanks
From this code I should assume that auser's UID is always the same as it's index.
If my assumption is wrong, this is a reason, You should ask for UserMap.get(temp_uid)
not UserMap.get(i)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.