Search code examples
androidparcel

Android Parcel Negative Array Size Exception


I am new to Android programming and of course Parcelable concept was totally new to me too..I got a Parcelable class called Store. However when running the following code, i got a negative array size exception. Spent a few hours already but couldn't really figure out what the problem is. Please have a look at the code below (i didn't take all my codes but) Please help!

public class Store implements Parcelable {

int id;
String storeAddress;
String storeCategory;
String storeContact;
String storeDescription;
String storeFacebook;
String storeFranchise;
String storeId;
Boolean storeIsActive;
Boolean storeIsSubscribed;
Double storeLatitude;
Double storeLongitude;
byte[] storeLogo;


@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(id);
    dest.writeString(storeAddress);
    dest.writeString(storeCategory);
    dest.writeString(storeContact);
    dest.writeString(storeDescription);
    dest.writeString(storeFacebook);
    dest.writeString(storeFranchise);
    dest.writeString(storeId);
    dest.writeInt(storeIsActive ? 1 : 0);
    dest.writeInt(storeIsSubscribed ? 1 : 0);
    dest.writeDouble(storeLatitude);
    dest.writeDouble(storeLongitude);
    dest.writeByteArray(storeLogo);
    dest.writeString(storeName);
    dest.writeString(storeWebSite);
}

public static final Parcelable.Creator<Store> CREATOR = new Parcelable.Creator<Store>() {
    public Store createFromParcel(Parcel in) {
        return new Store(in);
    }

    public Store[] newArray(int size) {
        return new Store[size];
    }
};

private Store(Parcel in) {

    id = in.readInt();
    storeAddress = in.readString();
    storeCategory = in.readString();
    storeContact = in.readString();
    storeDescription = in.readString();
    storeFacebook = in.readString();
    storeFranchise = in.readString();
    storeId = in.readString();
    storeIsActive = in.readInt() > 0;
    storeIsSubscribed = in.readInt() > 0;
    storeLatitude = in.readDouble();
    storeLongitude = in.readDouble();
    storeLogo = new byte[in.readInt()];
    in.readByteArray(storeLogo);
    storeName = in.readString();
    storeWebSite = in.readString();
}

This is the call stack.

Caused by: java.lang.NegativeArraySizeException: -1
        at com.testapp.app.Store.<init>(Store.java:97)
        at com.testapp.app.Store.<init>(Store.java:9)
        at com.testapp.app.Store$1.createFromParcel(Store.java:74)
        at com.testapp.app.Store$1.createFromParcel(Store.java:72)
        at android.os.Parcel.readParcelable(Parcel.java:2104)
        at android.os.Parcel.readValue(Parcel.java:2013)
        at android.os.Parcel.readListInternal(Parcel.java:2343)
        at android.os.Parcel.readArrayList(Parcel.java:1703)
        at android.os.Parcel.readValue(Parcel.java:2034)
        at android.os.Parcel.readArrayMapInternal(Parcel.java:2314)
        at android.os.Bundle.unparcel(Bundle.java:249)
        at android.os.Bundle.getString(Bundle.java:1118)
        at android.content.Intent.getStringExtra(Intent.java:4624)
        at com.testapp.app.StoresListActivity.onCreate(StoresListActivity.java:27)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)

Solution

  • private Store(Parcel in) {
         id = in.readInt();
         storeAddress = in.readString();
         storeCategory = in.readString();
         storeContact = in.readString();
         storeDescription = in.readString();
         storeFacebook = in.readString();
         storeFranchise = in.readString(); 
         storeId = in.readString(); 
         storeIsActive = in.readInt() > 0;     
         storeIsSubscribed = in.readInt() > 0;
         storeLatitude = in.readDouble();
         storeLongitude = in.readDouble();
         storeLogo =  in.readByteArray(); 
         storeName = in.readString();
         storeWebSite = in.readString();
     }