Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException trying to read Parcel String[]

I am getting a NullPointerException when I am trying to read back a String[] when I create an object from Parcel. Here is my code:

@Override
public void writeToParcel(Parcel out, int flags) {
    out.writeInt(floors);
    out.writeStringArray(indoorMaps);

}

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

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

private Building(Parcel in) {   
    floors = in.readInt();
    in.readStringArray(indoorMaps);
}

So indoorMaps is an attribute of my class, and it a String[], but I get the NullPointerException. I have checked the dev's documentation but there's nothing there. I followed this tutorial and they are using readStringArray in there.

Any suggestions? Thanks

like image 801
marimaf Avatar asked May 26 '12 20:05

marimaf


1 Answers

You are giving Parcel a null array when calling readStringArray. For it to work, you would have to initialize indoorMaps. You probably want createStringArray instead.

like image 121
K-ballo Avatar answered Nov 19 '22 04:11

K-ballo