Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volley onErrorResponse Give NullPointerException

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?

like image 219
user2940459 Avatar asked Oct 31 '13 07:10

user2940459


2 Answers

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.

like image 67
user2807662 Avatar answered Nov 17 '22 19:11

user2807662


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.

like image 3
Sarmad Shah Avatar answered Nov 17 '22 19:11

Sarmad Shah