1

The issue is following - I start the activity and when I want to take a image I call the camera (button onclick) with startActivityForResult(intent) and the intent with bitmap returns as normal where I assign the bitmap to a property in my CounterUser object (it's class implements parcelable) and at last show it on a ImageView - this works fine..but to handle the orientation change I added the following code in the activity - this causes and NullPointerException and closes the activity:

NOTE - counterObj is my instance of the CounterUser class which implements the Parcelable interface.

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putParcelable("counterObj", counterObj);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    if(savedInstanceState != null){
    counterObj = savedInstanceState.getParcelable("counterObj");
    counterImgView.setImageBitmap(counterObj.counterImg);
    }   
}

The error:

enter image description here

This is my class implementing the Parcelable interface - it has also a Bitmap property... it uses two constructor - In the activity I use the first no argument constructor to assign null values to all instance properties

public class CounterUser implements Parcelable {


String fname;
String lname;
String adresse;
Integer counterID;
Integer counterValue;
Bitmap counterImg;
Boolean damageExists;
String damageDescript;
//----
Double longitude;
Double latitude;
Integer workerID;
String updateDate;


public CounterUser(){

    this(null,null,null,null,null,null,null,null,null,null,null,null);  
}

public CounterUser(String fname, String lname, String adresse, Integer counterID, Integer counterValue, Bitmap counterImg, Boolean damageExists, 
        String damageDescript, Double longitude, Double latitude, Integer workerID, String updateDate){

    this.fname = fname;
    this.lname = lname;
    this.adresse = adresse;
    this.counterID = counterID;
    this.counterValue = counterValue;
    this.counterImg = counterImg;
    this.damageExists = damageExists;
    this.damageDescript = damageDescript;
    this.longitude = longitude;
    this.latitude = latitude;
    this.workerID = workerID;
    this.updateDate = updateDate;

}

@Override
public int describeContents() { 
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(this.fname);
    dest.writeString(this.lname);
    dest.writeString(this.adresse);
    dest.writeInt(this.counterID);
    dest.writeInt(this.counterValue);
    //Bitmap je parcelable
    this.counterImg.writeToParcel(dest, flags);

    dest.writeByte((byte) (this.damageExists ? 1 : 0)); 
    dest.writeString(this.damageDescript);
    dest.writeDouble(this.longitude);
    dest.writeDouble(this.latitude);
    dest.writeInt(this.workerID);
    dest.writeString(this.updateDate);

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

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

    private CounterUser(Parcel in) {

        this.fname = in.readString();
        this.lname = in.readString();
        this.adresse = in.readString();
        this.counterID = in.readInt();
        this.counterValue = in.readInt();
        //Bitmap je parcelable
        this.counterImg = Bitmap.CREATOR.createFromParcel(in);

        this.damageExists = in.readByte() == 1;
        this.damageDescript = in.readString();
        this.longitude = in.readDouble();
        this.latitude = in.readDouble();
        this.workerID = in.readInt();
        this.updateDate = in.readString();
    }

}

ArloGrant
  • 249
  • 5
  • 15
  • 3
    Upon a quick look, it looks like your default constructor sets every property to null. So counterImg is null and not a Bitmap. So, you are trying to call the writeToParcel function of null, which does not exist. – Matt Apr 26 '13 at 17:27
  • 2
    Something in CounterUser.java on line 58 is null – petey Apr 26 '13 at 17:27
  • Well yes, I am creating an instance of CounterUser in onCreate() with empty constructor meaning that all fields of the instance will be assigned with NULL....Because the activity is sort of Form ...editText, image, firsname, etc, chechboxes....so when the user enters in the edittext something it will assign that value to the appropriate instance property (counterObj.fname = "Mark"), etc...but how should I circumvent the non null instance properties of CounterUser for Bitmaps field, etc - or in some cases the user do not enter the value and that property of counterObj will be null. – ArloGrant Apr 26 '13 at 18:23
  • or better question would be if I allow null fields how should I handle them when saving the instance in onSaveInstanceState(Bundle outState) or when I send the instance to an other activity via intent? - thanks – ArloGrant Apr 26 '13 at 18:25

1 Answers1

0

How do you create the CounterUser instance? If you use the empty public constructor you have a lot of null member fields that will cause the writeToParcel to throw NPE. Either you don't allow null fields or you handle them when saving the instance.

forty-two
  • 12,204
  • 2
  • 26
  • 36
  • From the post: "In the activity I use the first no argument constructor to assign null values to all instance properties." It's most likely the attempt to write the Bitmap object to the parcel. – Matt Apr 26 '13 at 17:42
  • Well yes, I am creating an instance of CounterUser in onCreate() with empty constructor meaning that all fields of the instance will be assigned with NULL....Because the activity is sort of Form ...editText, image, firsname, etc, chechboxes....so when the user enters in the edittext something it will assign that value to the appropriate instance property (counterObj.fname = "Mark"), etc...but how should I circumvent the non null instance properties of CounterUser for Bitmaps field, etc - or in some cases the user do not enter the value and that property of counterObj will be null. – ArloGrant Apr 26 '13 at 18:18
  • or better question would be if I allow null fields how should I handle them when saving the instance in onSaveInstanceState(Bundle outState) or when I send the instance to an other activity via intent? - thanks – ArloGrant Apr 26 '13 at 18:23
  • @Matt. I missed that statement. Note to self: Read entire post before answering. But, it could be any field, really. – forty-two Apr 26 '13 at 19:59
  • 1
    @CicPot. Sorry, I don't know this API in enough detail to answer how to handle null fields, but I see that there is a `writeValue(Object)/Object readValue()` pair that obviously handles null values. Maybe something to look at. – forty-two Apr 26 '13 at 20:13
  • I will try something to do in writeToParcel() with if()..else - to avoid writing null values... – ArloGrant Apr 26 '13 at 20:38
  • Probably not! How would you know which fields are null when reading the object back? – forty-two Apr 26 '13 at 21:00
  • You are right - the problem would then be reading the Parcel. I really do not know because there is no writeNull/readNull - and I am not so experianced in this...I will have to dismiss this idea with parelable and do it as usual with Bundle. thanks anyway – ArloGrant Apr 27 '13 at 07:42
  • http://stackoverflow.com/questions/5905105/how-to-serialize-null-value-when-using-parcelable-interface here is something similar where they check null and write -1 or empty string. – ArloGrant Apr 27 '13 at 07:55