Error with Volley: Attempt to invoke virtual method 'com.android.volley.Request

0

I am working with an application that must connect with web services to be able to consult information from a remote DB, but when I press a button to launch my web service it gives me the following error:

  

Attempt to invoke virtual method 'com.android.volley.Request   com.android.volley.RequestQueue.add (com.android.volley.Request) 'on a   null object reference

I do not know why it gives this error, when my application starts loading a web service that starts without problems but when using the button to launch the next web service it is where it marks the error, this is the class where I am connecting with my Base of data:

public class noticiasWebService implements Response.Listener<JSONObject>, Response.ErrorListener {

    RequestQueue request;
    JsonObjectRequest jsonObjectRequest;

    public void update(String tituloNoticia, String subtituloNoticia, String imagenNoticia, String descripcionNoticia) {

        String url = "http://192.168.1.72:80/webservicemgrex/agregarFavoritos.php?tituloFavorito="+tituloNoticia+"" +
                "&subtituloFavorito="+subtituloNoticia+
                "&imagenFavorito="+imagenNoticia+
                "&descripcionFavoritos="+descripcionNoticia;

        url = url.replace(" ","%20");
        jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,url,null,this,this);
        request.add(jsonObjectRequest);
    }

    @Override
    public void onResponse(JSONObject response) {
        Log.i("Error","Se haregistrado con exito: "+response);
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        Log.i("Error","No se pudo consultar el registro: "+error.toString());
    }
}
    
asked by Enrique Espinosa 28.11.2018 в 17:47
source

1 answer

1

What happens is that you have declared your RequestQueue but you have not instantiated the Volley class, your code would look like this:

public class noticiasWebService implements Response.Listener<JSONObject>, Response.ErrorListener {

    RequestQueue request;
    JsonObjectRequest jsonObjectRequest;

    public void update(String tituloNoticia, String subtituloNoticia, String imagenNoticia, String descripcionNoticia, Context context) {

        String url = "http://192.168.1.72:80/webservicemgrex/agregarFavoritos.php?tituloFavorito="+tituloNoticia+"" +
                "&subtituloFavorito="+subtituloNoticia+
                "&imagenFavorito="+imagenNoticia+
                "&descripcionFavoritos="+descripcionNoticia;

        url = url.replace(" ","%20");
        request = Volley.newRequestQueue(context);
        jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,url,null,this,this);
        request.add(jsonObjectRequest);
    }

    @Override
    public void onResponse(JSONObject response) {
        Log.i("Error","Se haregistrado con exito: "+response);
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        Log.i("Error","No se pudo consultar el registro: "+error.toString());
    }
}
    
answered by 28.11.2018 / 19:10
source