0

I use the code below to get JSON data (img, name), and everything works fine, but I get a lot of report in Google Console under ANRs & Crashes :

java.lang.NullPointerException: 
  at maa.app.Fragments.fragmentAnime$3.onSuccess (fragmentAnime.java:75)
  at maa.app.Fragments.fragmentAnime$6.a (fragmentAnime.java:52)
  at maa.app.Fragments.fragmentAnime$6.onResponse (fragmentAnime.java:2)
  at com.android.volley.request.StringRequest.c (StringRequest.java:4)
  at com.android.volley.request.StringRequest.a (StringRequest.java:2)
  at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run (ExecutorDelivery.java:30)
  at android.os.Handler.handleCallback (Handler.java:789)
  at android.os.Handler.dispatchMessage (Handler.java:98)
  at android.os.Looper.loop (Looper.java:164)
  at android.app.ActivityThread.main (ActivityThread.java:6942)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:327)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1374)

myApplication :

public class MApplication extends MultiDexApplication {
    public static RequestQueue requestQueue;
    @Override
    public void onCreate() {
        super.onCreate();
        requestQueue = Volley.newRequestQueue(getApplicationContext());
    }

Fragment :

 StringRequest stringRequest = new StringRequest(Request.Method.GET,
                    URL_DATA,
                    s -> {
                        List<Post> listItems = new ArrayList<>();
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            JSONArray array = jsonObject.getJSONArray("bgs");
                            for (int i = 0; i < array.length(); i++) {
                                JSONObject o = array.getJSONObject(i);
                                Post item = new Post(
                                        o.optString("img"),
                                        o.optString("name")
                                );
                                listItems.add(item);
                            }
                            callback.onSuccess(listItems);
                        } catch (JSONException e) {
                            slowInternetConnection();
                        }
                    },
                    callback::onError) {
            };
            if (getActivity() != null && isAdded()) {
                stringRequest.setShouldCache(false);
                requestQueue.getCache().clear();
                requestQueue.add(stringRequest);
            }

can anyone help me to get resolve this issue?

  • 1
    Look at line 75 in `fragmentAnime.java` with a debugger. Which is that line in your code, and what is null on that line? – Andy Turner May 28 '19 at 20:17
  • @AndyTurner thank you for your comment under this line I have `callback.onSuccess(listItems);` using Interface I just pass the list items to use it in another activity, should I post it? – Mouaad Abdelghafour AITALI May 28 '19 at 20:25
  • 1
    Hi! Just a guess, but is 'callback' an object to callback to your activity? It CAN happen that the user has rotated the device (or something similar), so for that split second, callback is null. To help prevent this, do something like : if( isAdded() && callback != null) { callback.onSuceess() }. As an aside, if your fragment has no UI interface and you are just using it to make the volley call - perhaps consider using a ViewModel instead. – Zee May 29 '19 at 09:09

0 Answers0