Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hazelcast portable serialization - handling null properties

I'm using Hazelcast to store some data in a IMap but currently I'm facing a "problem" with null properties in the POJO I'm storing in the cache.

Imagine I have the following pojo:

public class TestPojo implements Portable {

    public static final int CLASS_ID = 1;

    private Long id;
    private String description;
    private Long value;

    @Override
    public int getFactoryId() {
        return 1;
    }

    @Override
    public int getClassId() {
        return CLASS_ID;
    }

    @Override
    public void writePortable(PortableWriter writer) throws IOException {
        writer.writeLong("id", id);
        writer.writeUTF("description", description);
        writer.writeLong("value", value);
    }

    @Override
    public void readPortable(PortableReader reader) throws IOException {
        id = reader.readLong("id");
        description = reader.readUTF("description");
        value = reader.readLong("value");
    }
}

The problem is while serializing this class that contains the property 'value' = null it will throw a NullPointerException because the writeLong method uses a primitive long.

[3.3-EA2] Exception while load all task:com.hazelcast.nio.serialization.HazelcastSerializationException: java.lang.NullPointerException

My question is: what should by the right way to handle this null values?

I'm using Hazelcast 3.3-EA2.

Thank you.

like image 770
pedrocgsousa Avatar asked Mar 23 '26 15:03

pedrocgsousa


1 Answers

Why do you want to use portable at all? (Identified)DataSerializable is less of a pain. Portable does not deal with null values, so it means that you need to convert to a string and then e.g. insert 'null' for null or '1234' for a number to deal with null. And with reading you read out the string, if it contains 'null' then you set null otherwise you e.g. do Long.parseLong.

So unless you have a very good reason, I would stay far away from it.

like image 75
pveentjer Avatar answered Mar 25 '26 04:03

pveentjer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!