1

When I am trying to access catagory array after this code it gives me nullpointerexception. I am returning this array to other class. So it also produce nullpointerexception.

 JsonObjectRequest req = new JsonObjectRequest(url, new JSONObject(),
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    // handle response
                    try {
                        JSONArray jarray=(JSONArray)response.get(item);
                        catagory=new String[jarray.length()][2];
                        //catagory_name=new String[jarray.length()];
                        for(int i=0;i<jarray.length();i++){
                            JSONObject jobj = (JSONObject) jarray
                                    .get(i);
                            catagory[i][1]=jobj.getString(sectionurl);
                            catagory[i][0]=jobj.getString(name);
                        }
                    }
                    catch (JSONException je){

                    }

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            // handle error
        }
    })

 String str=catagory[0][0];//AT THIS LINE AN EXCEPTION IS THROWN  
user3606902
  • 829
  • 1
  • 10
  • 25

1 Answers1

1

As Selvin said, you should understand, how threads work.

In line String str=catagory[0][0]; catagory is null.

Only after public void onResponse(JSONObject response) {...} it will have value, 'cause it's assync operation.