Requirement: Find out if price
is null. Since a primitive float data type cannot be checked for null since its always 0.0, I opted out to use Float
instead, as it can be checked for null.
public class QOptions implements Parcelable {
public String text;
public Float price;
}
protected QOptions(Parcel in) {
text = in.readString();
unit_price = in.readFloat();
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(this.text);
parcel.writeFloat(this.price);
}
However, since the class also implements Parcelable
, the writeToParcel
crashes with the following exception:
Attempt to invoke virtual method 'float java.lang.Float.floatValue()' on a null object reference
And the exception points to this line:
parcel.writeFloat(this.price);
How can I use the Float
data type along with writeToParcel and not cause the exception? Or is there a better way to accomplish my requirement? I just need the price to be null if it's null.
You can handle it in the below manner.
@Override
public void writeToParcel(Parcel dest, int flags) {
if (price == null) {
dest.writeByte((byte) (0x00));
} else {
dest.writeByte((byte) (0x01));
dest.writeFloat(price);
}
}
To read the value of float -
unit_price = in.readByte() == 0x00 ? null : in.readFloat();
Decimal types have a number of special values: NaN, negative and positive infinities. You can use those values to indicate null
:
if (price == null) {
parcel.writeFloat(Float.NaN);
} else {
parcel.writeFloat(price);
}
And when reading:
float p = parcel.readFloat();
if (Float.isNaN(p)) {
price = null;
} else {
price = p;
}
NaN means "not a number", so it kind-of fits thematically for serializing things.
Unlike the solution, provided by @Kapil G, this approach does not waste additional 4 bytes for nullability flag (each call to writeByte()
actually stores entire int
in Parcal for performance reasons).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With