22
import android.os.Parcel;
import android.os.Parcelable;

public class MClass implements Parcelable {
    private byte[] _byte;

    public MClass() {
    }

    public MClass(Parcel in) {
        readFromParcel(in);
    }


    public byte[] get_byte() {
        return _byte;
    }

    public void set_byte(byte[] _byte) {
        this._byte = _byte;
    }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeByteArray(_byte);
    }

    public void readFromParcel(Parcel in) {
        in.readByteArray(_byte); //LOE - Line Of Exception
    }

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

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

}

Whenever I am going to retrieve the bytes in my following array it is returning exception of NullPointerException. Can any one say what is the problem? What I am trying to do is to transfer a downloaded image bytes from one activity to another.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Debopam Mitra
  • 1,842
  • 4
  • 27
  • 51

2 Answers2

70

You never initialize the _byte array upon reading the parcel, therefore it is null.

What I'd do is, when you write your parcel, store the length of the byte array followed by the actual byte array. When you read the parcel, first read the length and initialize your _byte array to a new array of that size, then read in the byte array.


Code moved from comment

In write...

dest.writeInt(_byte.length); 
dest.writeByteArray(_byte); 

and in read...

_byte = new byte[in.readInt()]; 
in.readByteArray(_byte);
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
nEx.Software
  • 6,782
  • 1
  • 26
  • 34
  • do you mean something like: public void readFromParcel(Parcel in) { _byte = new byte[length_of_byte]; in.readByteArray(_byte); } – Debopam Mitra Jul 29 '12 at 13:21
  • 2
    In write... `dest.writeInt(_byte.length); dest.writeByteArray(_byte);` and in read... `_byte = new byte[in.readInt()]; in.readByteArray(_byte);` – nEx.Software Jul 29 '12 at 13:25
  • 1
    Implementation of writeByteArray already stores length of byteArray before it, so you don't need to write it again. Instead, read int to init byte array and then move pointer back with `dest.setDataPosition(dest.dataPosition() - 4);` – Piaf Jan 21 '17 at 02:31
19

A shorter solution without storing the byte arrays length:

dest.writeByteArray(byteArray);
byteArray = in.createByteArray();
user1185087
  • 4,468
  • 1
  • 30
  • 38
  • 4
    This really should be the answer, it works and it's simple and you simply replace one line with another line of code. I thought to use this when I saw it available in Android Studio but I couldn't help to think how very odd it looks, is this typical Java coding? What if there are multiple byte[], how does the Parceling process determine which is which? I guess I don't understand the Parceling process...I thought I did.... – JamisonMan111 May 03 '18 at 04:17