最近在編寫android程序
源碼是這樣的:
child5 = new ArrayList>();
childData5 = new HashMap();
childData5.put("health_person_txt_smoking", person.getFlag_smoking());// 同理
childData5.put("health_person_txt_smokingnum", smokingnum);// 同理
childData5.put("health_person_txt_drink", person.getFlag_drink());
childData5.put("health_person_txt_drinknum", drinknum);
childData5.put("health_person_txt_training", person.getFlag_training());// 同理
childData5.put("health_person_txt_trainingnum", trainingnum);// 同理
childData5.put("health_person_txt_sleep", person.getFlag_sleep());
childData5.put("health_person_txt_sleepnum", sleepnum);
child5.add(childData5);
在下面直接編寫map.get(key).tostring空指針異常。。。
//這樣直接寫get(key).toString();出現空指針異常該如何編寫這段代碼
String name=(String)childData1.get(health_person_txt_name).toString();
String sex=(String)childData1.get(health_person_txt_sex).toString();
String birthday_string=(String)childData1.get(health_person_txt_birthday).toString();
String blood=(String)childData1.get(health_person_txt_blood).toString();
String insurance=(String)childData1.get(health_person_txt_insurance).toString();
正確的編寫方式什麼???求各位老師幫忙解答
肯定是你getXXX返回了一個NULL,就沒有得到對象,再toString肯定會報null pointer了,你可以在toString之前判斷一下對象是否為空,不要寫成鏈式就行了。