Menu

#8 NullpointerException caused by empty elements

open
nobody
None
5
2010-01-20
2010-01-20
solaro
No

First of all: Very good work on this library, I love it, as it makes working with XML in Java really easy :)

The bug: when setting an element to "required=false" and trying to deserialize an empty value in the XML:
Detailed StackTrace: http://pastebin.com/m6b3bba48

To reproduce:
example class:
@Root
public class MessageWrapper {
@Element(required=false)
private String message;
// getters & setters omitted
// ...
}

try to deserialize this example XML:
<MessageWrapper>
<message/>
</MessageWrapper>

----------------------
Bugfix: (see attachment for project relative patch)
----------------------
file: org.simpleframework.xml.core.Composite.java
line number: 573
reason: source is null
fix: move lines 571 and 573 into the if statement.

source snippet after fix (lines 566-582):

private Object read(InputNode node, Object source, Label label) throws Exception {
Object object = readObject(node, source, label);

if(object == null) {
if(label.isRequired() && revision.isEqual()) {
Position line = node.getPosition();
Class type = source.getClass();
throw new ValueRequiredException("Empty value for %s in %s at %s", label, type, line);
}
} else {
if(object != label.getEmpty(context)) {
criteria.set(label, object);
}
}
return object;
}

Discussion

  • solaro

    solaro - 2010-01-20

    project relative patch (stream) for the nullpointer exception

     
  • solaro

    solaro - 2010-01-20

    Updated test case to reproduce null pointer

     
  • solaro

    solaro - 2010-01-20

    Error narrowed down to case with constructor injection. (see attached jar for JUnit4 test case to reproduce)

     

Log in to post a comment.

MongoDB Logo MongoDB