NullPointerException (etc) from Parcel.readException

benkc picture benkc · Sep 17, 2013 · Viewed 13k times · Source

Exceptions that look like this are confusing:

FATAL EXCEPTION: main
java.lang.NullPointerException
    at android.os.Parcel.readException(Parcel.java:1437)
    at android.os.Parcel.readException(Parcel.java:1385)
    at com.yourpackage.ipc.IYourClass$Stub$Proxy.yourMethod(IYourClass.java:488)
    at com.yourpackage.ipc.YourClassShim.yourMethod(YourClassShim.java:269)

I found a bunch of related questions for this, but none with the answer to "how do you debug this". So I'm making this Question/Answer.

By looking at the android source here and here you'll see that it can be throwing any of these (the NullPointerException is just what I had):

SecurityException(msg);
BadParcelableException(msg);
IllegalArgumentException(msg);
NullPointerException(msg);
IllegalStateException(msg);
RuntimeException("Unknown exception code: " + code + " msg " + msg);

But what's causing these?

Answer

benkc picture benkc · Sep 17, 2013

What's going on here is that readException() is checking the IPC byte stream for a header that says that an exception occurred; if it finds one, then it throws a new exception of that type, with the same message, but missing the original stack trace. (It only actually knows a few exception types; anything else gets translated into a base RuntimeException.)

So where's the original exception coming from? Well, somewhere down in the guts of the real implementation of YourClass.yourMethod() -- not in any of the parcelable or IPC code. So go there, wrap the whole method in a try/catch, and log whatever you caught.

(Or set a breakpoint there if you've got remote process breakpoints working.)