java - HashMap get an element and set into customized value type -
i have problem while set element , set customized value type. here 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 , 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, parse json array data, sure parsing json 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 exception java.lang.nullpointerexception
is @ line. usermap.get(i).setcheckin(inmap);
can initialize uid , set friendlist , checkinmap later? sounds wired. please give me hints or suggestions. thanks
from code should assume auser's uid same it's index.
if assumption wrong, reason, should ask usermap.get(temp_uid)
not usermap.get(i)
Comments
Post a Comment