title: NPE while using HashMap to populate ListView
body: <p>I am creating an android app for a restaurant that lets the waiters take the customers orders. I am using a extended list view to show the menu and have used a hashmap to populate the extended list view. When I run the app, there are no errors but the app crashes when trying to open the activity with the extended list view on it. After debugging, I found that there were null pointer exceptions causing it to crash but I'm struggling to fix it.
  Below is my code:</p>
  
  <p>Menu activity:</p>
  
  <pre><code>public class Menu extends AppCompatActivity {
  
      HashMap&lt;String, List&lt;String &gt;&gt; Food_menu;
      List&lt;String&gt; Food_list;
      ExpandableListView Exp_list;
      FoodMenuAdapter adapter;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
  
          Exp_list = (ExpandableListView) findViewById(R.id.exp_list);
          Food_menu = DataProvider.getInfo();
          Food_list = new ArrayList&lt;String&gt;(Food_menu.keySet());
          Exp_list.setAdapter(adapter);
          adapter = new FoodMenuAdapter(this, Food_menu, Food_list);
  
  
          Exp_list.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
              @Override
              public void onGroupExpand(int groupPosition)
              {
                  Toast pass2 = Toast.makeText(getBaseContext(),Food_list.get(groupPosition) + "is Expanded", Toast.LENGTH_SHORT);
                  pass2.show();;
              }
          });
  
  
      }
  }
  </code></pre>
  
  <p>FoodMenuAdapter Class:</p>
  
  <pre><code>public class FoodMenuAdapter  extends BaseExpandableListAdapter{
  
      private Context ctx;
      private HashMap&lt;String, List&lt;String&gt;&gt; Food_Category;
      private List&lt;String&gt; Food_List;
  
      public FoodMenuAdapter(Context ctx, HashMap&lt;String, List&lt;String&gt;&gt; Food_Category, List&lt;String&gt; Food_List)
      {
          this.ctx = ctx;
          this.Food_Category = Food_Category;
          this.Food_List = Food_List;
      }
  
  
      @Override
      public int getGroupCount()
      {
          return Food_List.size();
      }
  
      @Override
      public int getChildrenCount(int arg0)
      {
          return Food_Category.get(Food_List.get(arg0)).size();
      }
  
      @Override
      public Object getGroup(int arg0)
      {
          return Food_List.get(arg0);
      }
  
      @Override
      public Object getChild(int parent, int child)
      {
          return Food_Category.get(Food_List.get(parent)).get(child);
      }
  
      @Override
      public long getGroupId(int arg0)
      {
          return arg0;
      }
  
      @Override
      public long getChildId(int parent, int child)
      {
          return child;
      }
  
      @Override
      public boolean hasStableIds() {
          return false;
      }
  
      @Override
      public View getGroupView(int parent, boolean isExpanded, View convertView, ViewGroup parentView)
      {
          String group_title = (String) getGroup(parent);
          if (convertView == null)
          {
              LayoutInflater infalter = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              convertView = infalter.inflate(R.layout.parent_layout, parentView, false);
          }
          TextView parent_textview = (TextView) convertView.findViewById(R.id.parent_txt);
          parent_textview.setTypeface(null, Typeface.BOLD);
          parent_textview.setText(group_title);
          return convertView;
      }
  
      @Override
      public View getChildView(int parent, int child, boolean lastChild, View convertView, ViewGroup parentView)
      {
          String child_title = (String) getChild(parent, child);
          if(convertView == null)
          {
              LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              convertView = inflater.inflate(R.layout.child_layout, parentView, false);
  
          }
  
          TextView child_textview = (TextView) convertView.findViewById(R.id.child_txt);
          child_textview.setText(child_title);
  
          return convertView;
      }
  
      @Override
      public boolean isChildSelectable(int groupPosition, int childPosition)
      {
          return true;
      }
  }
  </code></pre>
  
  <p>DataProvider Class:</p>
  
  <pre><code>public class DataProvider {
  
      public static HashMap&lt;String, List&lt;String&gt;&gt; getInfo() {
          HashMap&lt;String, List&lt;String&gt;&gt; FoodDetails = new HashMap&lt;String, List&lt;String&gt;&gt;();
  
          List&lt;String&gt; Starter_Menu = new ArrayList&lt;String&gt;();
          Starter_Menu.add("Cheesy Garlic Bread");
          Starter_Menu.add("Liver Pate");
          Starter_Menu.add("Soup of the Day");
          Starter_Menu.add("Caesar Salad");
          Starter_Menu.add("Prawn Cocktail");
  
          List&lt;String&gt; Main_Menu = new ArrayList&lt;String&gt;();
          Main_Menu.add("Steak Burger");
          Main_Menu.add("Rib of Beef");
          Main_Menu.add("10oz Steak");
          Main_Menu.add("Pappardelle Pasta");
          Main_Menu.add("Fish and Chips");
  
          List&lt;String&gt; Desstert_Menu = new ArrayList&lt;String&gt;();
          Desstert_Menu.add("Sticky Toffee Pudding");
          Desstert_Menu.add("Chocolate Brownee with Ice Cream");
          Desstert_Menu.add("Cheesecake");
          Desstert_Menu.add("Ice Cream");
          Desstert_Menu.add("Strawberry Meringue");
  
          List&lt;String&gt; Sides_Menu = new ArrayList&lt;String&gt;();
          Sides_Menu.add("Onion Rings");
          Sides_Menu.add("Chips");
          Sides_Menu.add("Garlic Bread");
          Sides_Menu.add("Garlic Potatoes");
          Sides_Menu.add("Tobacco Onions");
  
          List&lt;String&gt; Drinks_Menu = new ArrayList&lt;String&gt;();
          Drinks_Menu.add("Tennants");
          Drinks_Menu.add("Coke");
          Drinks_Menu.add("Red Wine");
          Drinks_Menu.add("White Wine");
          Drinks_Menu.add("Guiness");
          Drinks_Menu.add("Magners");
  
          FoodDetails.put("Starter Menu", Starter_Menu);
          FoodDetails.put("Main Menu", Main_Menu);
          FoodDetails.put("Dessert Menu", Desstert_Menu);
          FoodDetails.put("Sides Menu", Sides_Menu);
          FoodDetails.put("Drinks Menu", Drinks_Menu);
  
          return FoodDetails;
      }
  }
  </code></pre>
  
  <p>It says in the Menu Activity that Exp_list, Food_menu and Food_list are all null. Does anyone see the problem or know how to fix it? </p>
  
  <h2>Edit</h2>
  
  <p>Logcat has been added</p>
  
  <pre><code>04-12 10:56:45.177 1984-1984/com.example.ben.restaurantapp E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.ben.restaurantapp, PID: 1984
      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ben.restaurantapp/com.example.ben.restaurantapp.Menu}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ExpandableListView.setAdapter(android.widget.ExpandableListAdapter)' on a null object reference
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
      at android.app.ActivityThread.access$800(ActivityThread.java:144)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:135)
      at android.app.ActivityThread.main(ActivityThread.java:5221)
      at java.lang.reflect.Method.invoke(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ExpandableListView.setAdapter(android.widget.ExpandableListAdapter)' on a null object reference
      at com.example.ben.restaurantapp.Menu.onCreate(Menu.java:39)
      at android.app.Activity.performCreate(Activity.java:5937)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
      at android.app.ActivityThread.access$800(ActivityThread.java:144)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:135)
      at android.app.ActivityThread.main(ActivityThread.java:5221)
      at java.lang.reflect.Method.invoke(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
  </code></pre>