有人可以告诉我为什么我得到null pointer exception
这个代码吗?在我的Nutrition
课堂上,我实现了一个hash map
:
public class Nutrition implements Serializable{
private final String TAG = Nutrition.class.getSimpleName();
//class variables
...
//hash map declaration
HashMap<String, String> nutrition;
public Nutrition(JSONObject jo) throws JSONException{
//create hash map
this.nutrition = new HashMap<String, String>();
//place data in hashmap
this.serving_size = jo.optString("serving_size");
nutrition.put("serving_size", serving_size);
Log.i(TAG, serving_size + " : Serving Size");
....
}
//method to get hashmap
public HashMap<String, String> getHashMap(){
return nutrition;
}
然后在另一堂课上,我打电话给getHashMap()
我null pointer
:
//nutrition item
Nutrition nutritionInfo = dish.getNutrition();
nutInfo = nutritionInfo.getHashMap();
有人可以告诉我我做错了什么以及如何解决吗?