pedrocgsousa
pedrocgsousa

Reputation: 417

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.

Upvotes: 3

Views: 3149

Answers (2)

Per-Åke Minborg
Per-Åke Minborg

Reputation: 290

Just check if (value != null) and only then call writer.writeLong(value).

Upvotes: 0

pveentjer
pveentjer

Reputation: 11307

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.

Upvotes: 1

Related Questions