google-code-export / morphia

Automatically exported from code.google.com/p/morphia
1 stars 0 forks source link

NullPointer with null element in array #392

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What version are you using? (Morphia/Driver/MongoDB)
0.99/2.6.5/2.0.1

Please include a stack trace below:
Caused by: java.lang.NullPointerException: null
        at com.google.code.morphia.mapping.EmbeddedMapper.writeCollection(EmbeddedMapper.java:61) [morphia-0.99.jar:na]
        at com.google.code.morphia.mapping.EmbeddedMapper.toDBObject(EmbeddedMapper.java:30) [morphia-0.99.jar:na]
        at com.google.code.morphia.mapping.Mapper.writeMappedField(Mapper.java:534) [morphia-0.99.jar:na]
        at com.google.code.morphia.mapping.Mapper.toDBObject(Mapper.java:442) [morphia-0.99.jar:na]
        ... 58 common frames omitted

I have null elements in my Java collection, which give a NullPointerException 
when persisted. It looks like it is the o.getClass() call on line 61 that is 
the problem. A simple null check (adding null to the List if the object is 
null) should work.

From line 61 onwards in the current trunk:
if (coll != null) {
    List values = new ArrayList();
    for (Object o : coll) {
         if (mapr.converters.hasSimpleValueConverter(mf) || mapr.converters.hasSimpleValueConverter(o.getClass())) { // Offending line
             ....
         }
         else {
             ....
         }
    }
    ....
}

Something like this should fix the problem:
if (coll != null) {
    List values = new ArrayList();
    for (Object o : coll) {
         if (o == null) { // Fix
             values.add(null); // Fix
         }
         else if (mapr.converters.hasSimpleValueConverter(mf) || mapr.converters.hasSimpleValueConverter(o.getClass())) {
             ....
         }
         else {
             ....
         }
    }
    ....
}

Thanks,
James

Original issue reported on code.google.com by jmcmi...@gmail.com on 22 Mar 2012 at 5:05

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r1822.

Original comment by scotthernandez on 13 Jul 2012 at 7:55