0
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.util.HashMap.toString()' on a null object reference

i was not able to show in my fragment android expandable using volley able to get response from server but it is showing error Null Pointer Exception

i was not able to show in my fragment android expandable using volley able to get response from server but it is showing error Null Pointer Exception

thank you

   public class NotificationFragment extends Fragment {
        private static String url = "http://www.androidbegin.com/tutorial/jsonparsetutorial.txt";
        private ProgressDialog mprocessingdialog;
        private TextView tv_welcomeuser, tv_recentupdate;
        private ExpandableListAdapter expandableListAdapter;
        private ExpandableListView exp_leaseoffer;
        private List<String> listDataHeader;
        private View rootView;
        private HashMap<String, List<String>> listDataChild;
        String jsonstr;
        RequestQueue requestQueue;





        public static NotificationFragment newInstance() {
            NotificationFrag

ment fragment = new NotificationFragment();
        return fragment;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_notification, container, false);



        tv_welcomeuser = (TextView) view.findViewById(R.id.tv_welcomeuser);
        tv_recentupdate = (TextView) view
                .findViewById(R.id.tv_recentupdate);

        exp_leaseoffer = (ExpandableListView) view.findViewById(R.id.lvExp);
        listDataHeader = new ArrayList<String>();

        new DownloadJason().execute();

        return view;
    }
    private class DownloadJason extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            mprocessingdialog = new ProgressDialog(getActivity());
            mprocessingdialog.setTitle("Please Wait..");
            mprocessingdialog.setMessage("Loading");
            mprocessingdialog.setIndeterminate(false);
            mprocessingdialog.show();


        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // TODO Auto-generated method stub

            StringRequest stringRequest = new StringRequest(Request.Method.GET,url,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            Log.d("rlog", response.toString());


                            jsonstr = response;


            if (jsonstr != null) {

                listDataHeader = new ArrayList<String>();
                listDataChild = new HashMap<String, List<String>>();

                try {
                    JSONObject jobj = new JSONObject(jsonstr);
                    JSONArray jarray = jobj.getJSONArray("worldpopulation");
                    for (int hk = 0; hk < jarray.length(); hk++) {
                        JSONObject d = jarray.getJSONObject(hk);
                        // Adding Header data

                        listDataHeader.add(d.getString("country"));
                        // Adding child data for lease offer
                        List<String> lease_offer = new ArrayList<String>();

                        lease_offer.add(d.getString("country") + "'s Rank is : "
                                + d.getString("rank"));
                        lease_offer.add("And Population is "+d.getString("population"));
                        // Header into Child data
                        listDataChild.put(listDataHeader.get(hk), lease_offer);

                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d("rlog", "Error: " + error.getMessage());

                }
            });

            requestQueue = Volley.newRequestQueue(getActivity());
            requestQueue.add(stringRequest);


            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            mprocessingdialog.dismiss();

            expandableListAdapter = new ExpandableAdapter(
                    getActivity(), listDataHeader, listDataChild);
            Log.i("groups", listDataHeader.toString());
            Log.i("details", listDataChild.toString());
            exp_leaseoffer.setAdapter(expandableListAdapter);

            exp_leaseoffer.setOnGroupClickListener(new OnGroupClickListener() {

                @Override
                public boolean onGroupClick(ExpandableListView parent, View v,
                                            int groupPosition, long id) {
                    // TODO Auto-generated method stub
                    return false;
                }

            });
            // Listview Group expanded listener
            exp_leaseoffer
                    .setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

                        @Override
                        public void onGroupExpand(int groupPosition) {

                        }
                    });
            exp_leaseoffer
                    .setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

                        @Override
                        public void onGroupCollapse(int groupPosition) {
                            Toast.makeText(
                                    getActivity().getApplicationContext(),
                                    listDataHeader.get(groupPosition)
                                            + " Collapsed", Toast.LENGTH_SHORT)
                                    .show();
                        }
                    });
            exp_leaseoffer.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

                @Override
                public boolean onChildClick(ExpandableListView parent, View v,
                                            int groupPosition, int childPosition, long id) {


                    return false;
                }
            });

        }
    }


    }
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – HaveSpacesuit Jul 27 '17 at 14:36

1 Answers1

0

initialize & declare hashmap at the top

 private HashMap<String, List<String>> listDataChild = new  HashMap<String, List<String>> ();
Yogesh Mane
  • 403
  • 6
  • 18