Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException in hashCode() when deserializing an object graph

I have a final field in a Java class that is sure to be initialized. When I serialize and deserialize a corresponding object graph, I get a NPE because the field is used in the hashCode() method but apparently not yet read back. I created a minimal test case that also contains the serialize()/deserialize() methods (in case those are buggy), but I can't seem to be able to understand what the (underlying) problem is or how to work around it.

public class TestSerializerTest {

private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(TestSerializerTest.class);

private File workDir;

@Before
public void setUp() {
    workDir = new File("target/tmp");
}

@After
public void tearDown() {
    workDir.delete();
}

private static class ManagingContainer implements Serializable {

    private static final long serialVersionUID = 1L;

    private final Set<Contained> containedElements = new HashSet<Contained>();

    public Contained getContained(List<String> descriptors) {
        Contained contained = new Contained(this, descriptors);
        containedElements.add(contained);
        return contained;
    }
}

private static class Contained implements Serializable {

    private static final long serialVersionUID = 1L;

    @SuppressWarnings("unused")
    private final ManagingContainer container;
    private final List<String> descriptors;

    Contained(ManagingContainer container, List<String> descriptors) {
        this.container = container;
        if (descriptors == null) {
            throw new NullPointerException();
        }
        this.descriptors = new ArrayList<String>(descriptors);
    }

    @Override
    public int hashCode() {
        return descriptors.hashCode();
    }

    @Override
    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }
        if (other == null || !(other instanceof Contained)) {
            return false;
        }
        return this.descriptors.equals(((Contained) other).descriptors);
    }
}

private static class OtherContainer implements Serializable {
    private static final long serialVersionUID = 1L;

    private final ArrayList<Contained> containedElements = new ArrayList<Contained>();

    public OtherContainer(Contained initialElement) {
        this.containedElements.add(initialElement);
    }

    public void addContained(Contained nextElement) {
        containedElements.add(nextElement);
    }
}

void serializeObjectToFile(Serializable serializable, File file) {
    logger.info("Saving object '{}' to file '{}'.", serializable, file.getAbsolutePath());
    ObjectOutputStream stream = null;
    try {
        if (!file.getParentFile().exists()) {
            logger.info("Creating directory '{}'.", file.getParentFile().getAbsolutePath());
            file.getParentFile().mkdirs();
        }
        stream = new ObjectOutputStream(new FileOutputStream(file));
        stream.writeObject(serializable);
        stream.flush();
    } catch (Exception exception) {
        throw new RuntimeException(exception);
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException exc) {
                logger.error("Error closing stream.", exc);
            }
        }
    }
}

Object deserializeObjectFromFile(File file) {
    ObjectInputStream oiStream = null;
    try {
        logger.info("Loading object from file '{}'.", file.getAbsolutePath());
        oiStream = new ObjectInputStream(new FileInputStream(file));
        return oiStream.readObject();
    } catch (Exception exc) {
        logger.error("Exception loading object from file '{}'. Ignoring file!", file.getAbsolutePath(), exc);
        throw new RuntimeException(exc);
    } finally {
        if (oiStream != null) {
            try {
                oiStream.close();
            } catch (IOException exc) {
                logger.error("Error closing stream.", exc);
            }
        }
    }
}

@Test
public void testSerializeDeserialize() {
    ManagingContainer container = new ManagingContainer();
    OtherContainer serializable = new OtherContainer(container.getContained(new ArrayList<String>()));
    serializable.addContained(container.getContained(new ArrayList<String>()));
    File file = new File(workDir, "test.ser");
    serializeObjectToFile(serializable, file);
    OtherContainer result = (OtherContainer) deserializeObjectFromFile(file);
}
}

Executing this test creates the following NullPointerException:

java.lang.NullPointerException: null
at TestSerializerTest$Contained.hashCode(TestSerializerTest.java:67) ~[test-classes/:na]
at java.util.HashMap.put(HashMap.java:372) ~[na:1.6.0_29]
at java.util.HashSet.readObject(HashSet.java:292) ~[na:1.6.0_29]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_29]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_29]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_29]
at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_29]
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1848) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328) ~[na:1.6.0_29]
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1946) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1870) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328) ~[na:1.6.0_29]
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1946) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1870) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350) ~[na:1.6.0_29]
at java.util.LinkedList.readObject(LinkedList.java:964) ~[na:1.6.0_29]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_29]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_29]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_29]
at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_29]
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1848) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328) ~[na:1.6.0_29]
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1946) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1870) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350) ~[na:1.6.0_29]
at TestSerializerTest.deserializeObjectFromFile(TestSerializerTest.java:128) [test-classes/:na]
at TestSerializerTest.testSerializeDeserialize(TestSerializerTest.java:151) [test-classes/:na]

As far as I understand serialization, this should not be a problem (see also Does Java Serialization work for cyclic references? and https://softwareengineering.stackexchange.com/questions/151055/what-happens-if-we-serialize-and-deserialize-two-objects-which-references-to-eac).

Currently I have no idea how to proceed. Any hints are highly appreciated! Thanks!

like image 617
roesslerj Avatar asked Nov 17 '14 10:11

roesslerj


2 Answers

The problem seems to be caused by the circular relationship between ManagingContainer and Container. When Container is deserialized it in turn deserializes the ManagingContainer referenced by the 'container' property. However, as this deserializes it tries to populate the HashSet with the Container which is in the process of being deserialized.

If you serialize/deserialize instead your ManagingContainer it should work fine as the Containers would be fully loaded before hashCode() is called. Alternatively, rethink your object graph to remove the circular dependency or write custom object read/write methods.

like image 76
BarrySW19 Avatar answered Oct 11 '22 15:10

BarrySW19


I encountered this issue with a complex object graph had bi-directional circular references using HashMaps. Since the key of the HashMap had not fully loaded when hashCode() was called on it, I also got a NullPointerException:

private String id;

int hashCode() {
    return id.hashCode(); // NPE during deserialization
}

I "fixed" (read: "avoided") the problem by also caching and serializing the hashCode int itself:

private String id;
private int hashCode;

public void setId( String id ) {
  this.id = id;
  this.hashCode = id.hashCode();
}

public int hashCode() {
  return this.hashCode;
}

Primitives are in-lined during serialization. They're loaded immediately, before any object references, such as the id String above. So the hashCode int will be available even if no referenced Objects have loaded.

Bad news, this does mean that hashCode will need to be updated whenever the hashCode-providing Object is. For Objects with clearly defined access/update, such as immutable Strings, this is pretty straightforward (see setId(...) above). For Objects with complex interactions, like Collections, this is harder to control, since it may depend on changes to the contents of the Collection and also the hashCodes of the individual Objects in the Collection.

Good news, as long as memory is cheap, this may lead to a slight performance improvement if hashCode() is called repeatedly.

like image 36
Jason Greanya Avatar answered Oct 11 '22 16:10

Jason Greanya