How to serialize null value when using Parcelable interface

igor.beslic picture igor.beslic · May 6, 2011 · Viewed 27.5k times · Source

regarding my code example down, what shold I do if one Locable's variables is null? In example, now if l.getZoom() returns null, I got NullPointerException.

@Override
public void writeToParcel(Parcel parcel, int arg1) {
    parcel.writeInt(count);
    for(Locable l:locableArr){
        parcel.writeInt(l.getOriginId());
        parcel.writeInt(l.getLocableType());
        parcel.writeInt(l.getZoom());
        parcel.writeDouble(l.getLatituda());
        parcel.writeDouble(l.getLongituda());
        parcel.writeString(l.getTitle());
        parcel.writeString(l.getSnipet());
    }

}

Thanks!

Answer

artch picture artch · May 26, 2012

You can use Parcel.writeValue for marshalling generic object with null value.