Volley onErrorResponse Give NullPointerException
Asked Answered
A

3

5

i try volley library in my android application

this is my log

    10-31 14:30:09.277: E/AndroidRuntime(22916): java.lang.NullPointerException
10-31 14:30:09.277: E/AndroidRuntime(22916):    at com.mypackage.api.Api$2.onErrorResponse(Api.java:269)
10-31 14:30:09.277: E/AndroidRuntime(22916):    at com.android.volley.Request.deliverError(Request.java:517)
10-31 14:30:09.277: E/AndroidRuntime(22916):    at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:101)
10-31 14:30:09.277: E/AndroidRuntime(22916):    at android.os.Handler.handleCallback(Handler.java:615)
10-31 14:30:09.277: E/AndroidRuntime(22916):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-31 14:30:09.277: E/AndroidRuntime(22916):    at android.os.Looper.loop(Looper.java:137)
10-31 14:30:09.277: E/AndroidRuntime(22916):    at android.app.ActivityThread.main(ActivityThread.java:4745)
10-31 14:30:09.277: E/AndroidRuntime(22916):    at java.lang.reflect.Method.invokeNative(Native Method)
10-31 14:30:09.277: E/AndroidRuntime(22916):    at java.lang.reflect.Method.invoke(Method.java:511)
10-31 14:30:09.277: E/AndroidRuntime(22916):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-31 14:30:09.277: E/AndroidRuntime(22916):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-31 14:30:09.277: E/AndroidRuntime(22916):    at dalvik.system.NativeStart.main(Native Method)

this is how i use volley

    GetStringRequest req = new GetStringRequest(Request.Method.GET,URL_API,
    new Response.Listener<String>() {
    // handle success response
    }, new Response.ErrorListener() {
    //handle error response
    @Override
public void onErrorResponse(VolleyError volleyError) {

    try {
        String error = new String(volleyError.networkResponse.data, HTTP.UTF_8);
        }
        catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
        e.printStackTrace();
        }
    });

sometimes i get error nullPointerException at this line (269)

String error = new String(volleyError.networkResponse.data, HTTP.UTF_8);

i don't know what is wrong, anyone know?

Aguiar answered 31/10, 2013 at 7:47 Comment(7)
that line is String error = new String(volleyError.networkResponse.data, HTTP.UTF_8);Aguiar
You can try to check whether volleyError, volleyError.networkResponse or volleyError.networkResponse.data is null.Kelci
I've been doing it, but the error still appearsAguiar
which variable is null ?Kelci
i dont know, when i check if(!volleyError.equals(null)) give nullPointerException.. same for vollerError.networkResponse and volleyError.networkResponse.data give nullPointerException tooAguiar
You'd better learn Java. You should check null by if(volleyError != null)Kelci
I know it is null put it should not be null because I want to see the errorAutoerotic
M
4

Chances are that volleyError.networkResponse.data is empty. I am not sure what you are trying to get with this line of code, but working with Volley and wanting to see what is in volleyError. You could try this:

String error =  volleyError.toString();

You can then check this string for any specific errors [at least that's how I do it]. VolleyErrors could be one of the few defined by the API such as timeout error, connection error, server error, and so forth. Of-course, you might have to parse the string further if you want to fire other actions based on a specific error.

Megargee answered 1/11, 2013 at 2:56 Comment(1)
My app tries to get volleyError.networkResponse.statusCode in order to inform the user whether the error was server crash (500) or invalid request (403) or not found (404) in case of an error. Thats the reason (for example)Kristoferkristoffer
P
3

It seems like onErrorResponse responds differently on few devices

onErrorResponse returned null on few devices (that was the reason for the crash)

  new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    if(error.getMessage==NULL){
  Toast.makeText(cardview.this, "Failed to retrieve data", Toast.LENGTH_LONG).show();
                    }
                else{
                        Toast.makeText(cardview.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
                    }

                });

I also had the same error..it varies from device to device ...you may find that it won't give nullpointer exception on some devices.

Polymorphonuclear answered 21/10, 2016 at 6:27 Comment(0)
M
0

Apply this code hope that this will help

@Override public void onErrorResponse(VolleyError volleyError) {

  Log.v("VolleyError",volleyError.getMessage);

});

This will print error in your log

Mccants answered 31/3, 2017 at 4:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.