Martin Golpashin Martin Golpashin - 3 months ago 8
Android Question

Nullpointerexception in Volley JsonObjectRequest

I am stuck trying to perform a simple JsonObjectRequest inside the Genymotion Emulator using Google volley. But the last line of the code below (instantiation of

JsonObjectRequest
) leads to a Nullpointerexception.

Could anyone please explain what I am missing here?

JSONObject requestParams = new JSONObject();
try{
requestParams.put("username","aaaa");
requestParams.put("password","bbbb");
}catch(JSONException e){
e.printStackTrace();
}

Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//Do something
}
};

Response.ErrorListener errorListener = new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError volleyError) {
}
};

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, "192.168.178.155/user", requestParams, listener, errorListener);


Logcat

java.lang.NullPointerException
at com.android.volley.Request.<init>(Request.java:129)
at com.android.volley.toolbox.JsonRequest.<init>(JsonRequest.java:58)
at com.android.volley.toolbox.JsonObjectRequest.<init>(JsonObjectRequest.java:48)
at my.package.name.RequestHandler.performRequest(RequestHandler.java:68)

Answer

You need to include http:// in your URL:

i.e. do

request = new JsonObjectRequest(Request.Method.GET, "http://192.168.178.155/user", requestParams, listener, errorListener); 

instead of

request = new JsonObjectRequest(Request.Method.GET, "192.168.178.155/user", requestParams, listener, errorListener);