title: hashmap to show data in expandable listview in android
body: <p>I am trying to show data in an expandable listview from server in android. I am trying to implement hashmap within the <code>AsyncTask</code>. Please see the following code.</p>
  
  <pre><code>private class HttpAsyncTaskReports extends AsyncTask&lt;String, Void, String&gt; {
      private ProgressDialog dialog;
      public HttpAsyncTaskReports(ExpandableActivity activity) {
          dialog = new ProgressDialog(activity);
      }
  
      @Override
      protected String doInBackground(String... urls) {
          return GET(urls[0]);
      }
  
      @Override
      protected void onPreExecute() {
          // Removes all markers, overlays, and polylines from the map.
          dialog.setMessage("Loading reports, please wait.");
          dialog.show();
      }
  
      @Override
      protected void onPostExecute(String result) {
  
          if (dialog.isShowing()) {
              dialog.dismiss();
          }
  
          listDataHeader = new ArrayList&lt;String&gt;();
          listDataChild = new HashMap&lt;String, List&lt;String&gt;&gt;();
  
          // Header, Child data
  
          List&lt;String&gt; top250 = new ArrayList&lt;String&gt;();
          top250.add("The Shawshank Redemption");
          top250.add("The Godfather");
  
          try {
              JSONArray jArray = new JSONArray(result);
  
              JSONObject jsonobject = jArray.getJSONObject(0);
              JSONArray jsonAssets = jsonobject.getJSONArray("last_data");
              Log.d("LASTLATLONG", result);
  
              for (i = 0; i &lt; jsonAssets.length(); i++) {
                  JSONObject nlmJsonObject = jsonAssets.getJSONObject(i);
                  JSONObject lastData_JsonObject = nlmJsonObject.getJSONObject("last_data");
                  JSONObject lastData_timeJsonObject = lastData_JsonObject.getJSONObject("time");
  //              JSONObject lastData_dateJsonObject = lastData_timeJsonObject.getJSONObject("$date");
  //              JSONObject lastData_speedJsonObject = lastData_JsonObject.getJSONObject("speed");
  
                  // Adding child data
                  listDataHeader.add(lastData_timeJsonObject.getString("$date"));
                  listDataChild.put(listDataHeader.get(i), top250);
              }
          } catch (JSONException e) {
              e.printStackTrace();
              Toast.makeText(getApplicationContext(), "Server Error :( Please wait!", Toast.LENGTH_LONG).show();
          }
      }
  }
  </code></pre>
  
  <p>Here is the <code>ExpandableListAdapter</code> class</p>
  
  <pre><code>public class ExpandableListAdapter extends BaseExpandableListAdapter {
  
      private Context _context;
      private List&lt;String&gt; _listDataHeader; // header titles
      // child data in format of header title, child title
      private HashMap&lt;String, List&lt;String&gt;&gt; _listDataChild;
  
      public ExpandableListAdapter(Context context, List&lt;String&gt; listDataHeader,
                                   HashMap&lt;String, List&lt;String&gt;&gt; listChildData) {
          this._context = context;
          this._listDataHeader = listDataHeader;
          this._listDataChild = listChildData;
      }
  
      @Override
      public Object getChild(int groupPosition, int childPosititon) {
          return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                  .get(childPosititon);
      }
  
      @Override
      public long getChildId(int groupPosition, int childPosition) {
          return childPosition;
      }
  
      @Override
      public View getChildView(int groupPosition, final int childPosition,
                               boolean isLastChild, View convertView, ViewGroup parent) {
  
          final String childText = (String) getChild(groupPosition, childPosition);
  
          if (convertView == null) {
              LayoutInflater infalInflater = (LayoutInflater) this._context
                      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              convertView = infalInflater.inflate(R.layout.list_item, null);
          }
  
          TextView txtListChild = (TextView) convertView
                  .findViewById(R.id.lblListItem);
  
          txtListChild.setText(childText);
          return convertView;
      }
  
      @Override
      public int getChildrenCount(int groupPosition) {
          return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                  .size();
      }
  
      @Override
      public Object getGroup(int groupPosition) {
          return this._listDataHeader.get(groupPosition);
      }
  
      @Override
      public int getGroupCount() {
          return this._listDataHeader.size();
      }
  
      @Override
      public long getGroupId(int groupPosition) {
          return groupPosition;
      }
  
      @Override
      public View getGroupView(int groupPosition, boolean isExpanded,
                               View convertView, ViewGroup parent) {
          String headerTitle = (String) getGroup(groupPosition);
          if (convertView == null) {
              LayoutInflater infalInflater = (LayoutInflater) this._context
                      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              convertView = infalInflater.inflate(R.layout.list_group, null);
          }
  
          TextView lblListHeader = (TextView) convertView
                  .findViewById(R.id.lblListHeader);
          lblListHeader.setTypeface(null, Typeface.BOLD);
          lblListHeader.setText(headerTitle);
  
          return convertView;
      }
  
      @Override
      public boolean hasStableIds() {
          return false;
      }
  
      @Override
      public boolean isChildSelectable(int groupPosition, int childPosition) {
          return true;
      }
  }
  </code></pre>
  
  <p>Here is the error....</p>
  
  <pre><code>java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3124)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3233)
      at android.app.ActivityThread.access$1000(ActivityThread.java:197)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:145)
      at android.app.ActivityThread.main(ActivityThread.java:6856)
      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:1404)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
  Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
      at gps.finder.com.findermaterial.ExpandableListAdapter.getGroupCount(ExpandableListAdapter.java:75)
      at android.widget.ExpandableListConnector.getCount(ExpandableListConnector.java:397)
      at android.widget.ListView.setAdapter(ListView.java:502)
      at android.widget.ExpandableListView.setAdapter(ExpandableListView.java:604)
      at gps.finder.com.findermaterial.ExpandableActivity.onCreate(ExpandableActivity.java:102)
      at android.app.Activity.performCreate(Activity.java:6550)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3077)
  </code></pre>
  
  <p>Can anybody tell me how should I solve this issue?</p>
  
  <p>If I use a function like the following one to list data, it works successfully.</p>
  
  <pre><code>private void prepareListData() {
      listDataHeader = new ArrayList&lt;String&gt;();
      listDataChild = new HashMap&lt;String, List&lt;String&gt;&gt;();
  
      // Adding child data
      listDataHeader.add("Top 50");
      listDataHeader.add("Now Showing");
      listDataHeader.add("Coming Soon..");
  
      // Adding child data
      List&lt;String&gt; top250 = new ArrayList&lt;String&gt;();
      top250.add("The Shawshank Redemption");
      top250.add("The Godfather");
      top250.add("The Godfather: Part II");
      top250.add("Pulp Fiction");
      top250.add("The Good, the Bad and the Ugly");
      top250.add("The Dark Knight");
      top250.add("12 Angry Men");
  
      List&lt;String&gt; nowShowing = new ArrayList&lt;String&gt;();
      nowShowing.add("The Conjuring");
      nowShowing.add("Despicable Me 2");
      nowShowing.add("Turbo");
      nowShowing.add("Grown Ups 2");
      nowShowing.add("Red 2");
      nowShowing.add("The Wolverine");
  
      List&lt;String&gt; comingSoon = new ArrayList&lt;String&gt;();
      comingSoon.add("2 Guns");
      comingSoon.add("The Smurfs 2");
      comingSoon.add("The Spectacular Now");
      comingSoon.add("The Canyons");
      comingSoon.add("Europa Report");
  
      listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
      listDataChild.put(listDataHeader.get(1), nowShowing);
      listDataChild.put(listDataHeader.get(2), comingSoon);
  }
  </code></pre>
  
  <p>But I need to implement the hashmap using the data retrieve from server.</p>
  
  <p>I appreciate your suggestions. Thanks in advance.</p>