I have an application which interacts with a custom service via two aidl implementations. When the service is ran, I am running into the following exception:
ERROR/AndroidRuntime(9435): FATAL EXCEPTION: main
ERROR/AndroidRuntime(9435): java.lang.NullPointerException
ERROR/AndroidRuntime(9435): at android.os.Parcel.readException(Parcel.java:1328)
ERROR/AndroidRuntime(9435): at android.os.Parcel.readException(Parcel.java:1276)
ERROR/AndroidRuntime(9435): at myservice.IAsyncService$Stub$Proxy.addItems(IAsyncService.java:259)
From this error, I think it is something to do with the marshalling which is carried out. So I checked both aidl implementations; the first aidl (which uses primitives) seemed to work fine (I think because they dont need me to specify imports/implementations); the second (which uses a custom object) however seems to crash giving the above error. I have made sure the custom object correctly implements parcelable (see bottom of post for implementation), and have even tried referencing the custom object from a separate aidl file as shown below.
package myservice;
parcelable myservice.MyItem;
package myservice;
import myservice.IMyItem;
interface IAsyncService
{
void addItems(in List<MyItem> itemCollection);
}
public class MyItem implements Parcelable
{
private String name = "";
public MyItem(String name)
{
this.name = name.trim();
}
public MyItem(Parcel source) {
readFromParcel(source);
}
public void setItem(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
public static final Parcelable.Creator<MyItem> CREATOR = new Parcelable.Creator<MyItem>() {
@Override
public MyItem[] newArray(int size) {
return new MyItem[size];
}
@Override
public MyItem createFromParcel(Parcel source) {
return new MyItem(source);
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel destination, int flags) {
destination.writeString(this.name);
}
public void readFromParcel(Parcel source) {
this.name = source.readString();
}
}
Is there something I am missing or have completely overlooked?
Thanks John
What you are seeing is that a NullPointerException has been thrown on the remote side, that exception was returned as a result, and re-thrown to your client. In these situations it helps a lot for debugging to find out what the original exception is. You can do this with a trick:
In your class that implements the interface, override onTransact() to do this:
protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) {
try {
super.onTransact(code, data, reply, flags);
} catch (RuntimeException e) {
Log.w("MyClass", "Unexpected remote exception", e);
throw e;
}
}
Now when the crash happens you will also see in the log the original exception printed, with the line where it happened.