0

I am using Json parsing using Volley concept,I got null pointer Exception this line "AppController.getInstance().addToRequestQueue(jsonObjReq);" please give a solution. thank you

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,Allbooksurl, null, new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject object) {
        try {
            String result = object.getString("status");
                            Log.d(result, "result");
                            String message = object.getString("info");
                            Log.d(message, "message");
                            JSONArray responserecentbooks= object.getJSONArray("recentbooks");
                            for (int i = 0; i < responserecentbooks.length(); i++)
                            {
                                JSONObject person = (JSONObject) responserecentbooks.get(i);
                                String id = person.getString("id");
                                String name = person.getString("name");
                                    db.insertValue(id, name);

 }
         } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        //VolleyLog.d(TAG, "Error: " + error.getMessage());
        Toast.makeText(getApplicationContext(),
        error.getMessage(), Toast.LENGTH_SHORT).show();
    }
});

try {
    AppController.getInstance().addToRequestQueue(jsonObjReq);
    } catch (Exception e) {
    e.printStackTrace();
}
MurugananthamS
  • 2,395
  • 4
  • 20
  • 49

3 Answers3

1

Your error clearly says that Null pointer Exception at

AppController.getInstance().addToRequestQueue(jsonObjReq);

So your instance of AppController class may be null in getInstance() method.

Piyush
  • 18,895
  • 5
  • 32
  • 63
1

Your AppController can be null. Check your code, should be like:

    private static AppController INSTANCE;
    public static AppController getInstance() {
        return INSTANCE;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        INSTANCE = this;
    }
xUser
  • 81
  • 6
0
 I forget to declare this in manifeast
    <application
    android:name="com.example.AppController"
    android:allowBackup="true"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:screenOrientation="portrait"></application>
MurugananthamS
  • 2,395
  • 4
  • 20
  • 49