I keep getting com.android.volley.RequestQueue.add on a null object reference error

Etoile :

I need to display a a list of products from a JSON file, but I keep getting this error:

https://i.imgur.com/WEu4DzV.png

I tried moving everything in my code (so maybe I messed it up a little) and I can't get it to work. I'm a beginner, so errors like this are very hard to fix for me.

This is the fragment that produces the error:

package com.example.app;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


public class ListFragment extends Fragment {

private RecyclerView mRecyclerView;
private ListAdapter mListAdapter;
private ArrayList<Item> mList;
private RequestQueue mRequestQueue;
Context context;


@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_list, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRecyclerView = view.findViewById(R.id.recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(mListAdapter);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        parseJSON();
        super.onActivityCreated(savedInstanceState);
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mList = new ArrayList<>();
        mListAdapter = new ListAdapter(context, mList);
}


private void parseJSON() {

        String url = "http://my_ip_adress/sestavsisvujsvetweb/api/seznammagnetek";
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                int number = 0;
                try {
                    number = response.getInt("monumentid1");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String name = null;
                try {
                    name = response.getString("name1");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String region = null;
                try {
                    region = response.getString("region1");
                } catch (JSONException e) {
                e.printStackTrace();
                }

                mRequestQueue = Volley.newRequestQueue(context);
                mList.add(new Item(number, name, region));

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });
        mRequestQueue.add(request); //this causes the problem, why? :(


}

}

Yunus Kulyyev :

The problem occurs because you are trying to add request into the requestQueue before knowing that it successfully retrieved your information. First, check if JsonObjectRequest has a onSuccess function, if not, then you need to add to requestQueue inside of the onResponse. Otherwise, android is synchronous so it will execute your line before actually retrieving the Json.

So the quick fix will be this:

if(request != null) {
   mRequestQueue.add(request);
}

This will make sure to not add if its null. Add this in your onCreate:

mRequestQueue = Volley.newRequestQueue(context);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=161795&siteId=1