0

I am attempting to use ModelMapper to map the following json as explained here http://modelmapper.org/user-manual/gson-integration/ but I am getting a NullPointerException and I can't figure what is wrong. Any tips please?

{"a": "aaa", "b": [{"c": "ccc"}]}   

public class Foo {
  private String a;
  private ArrayList<Bar> b;
}



public class Bar {
  private String c;
}

ModelMapper mapper = new ModelMapper();
mapper.getConfiguration().addValueReader(new JsonElementValueReader());
JsonElement responseElement = new JsonParser().parse(json);
Foo foo = mapper.map(responseElement, Foo.class);
dmz73
  • 1,588
  • 4
  • 20
  • 32
  • Where do you get a `NullPointerException`? – Sotirios Delimanolis Sep 20 '14 at 16:51
  • When posting "why do I get this error" type questions, you need (at a minimum) to post the full stack trace, and indicate which line in your code is throwing the exception. Even better would be to post a [SSCCE](http://sscce.org/). – azurefrog Sep 20 '14 at 16:56
  • This is the stack trace I get: https://gist.github.com/dabd/1b84bab88144ff6bc873#file-gistfile1-txt – dmz73 Sep 21 '14 at 10:00
  • With that JSON, `String json = "{\"a\": \"aaa\", \"b\": [{\"c\": \"ccc\"}]}";` and that code, things run just fine. Perhaps you could post a complete test that is failing? – Jonathan Sep 21 '14 at 16:10
  • I made an even simpler Json and switched to Jackson. Here is a complete test that fails: https://gist.github.com/dabd/e912418bd8105f3bd7ec#file-gistfile1-java. This is the pom I am using https://gist.github.com/dabd/cee26e64cf6c7df6bf5d#file-gistfile1-xml. Stack trace: https://gist.github.com/dabd/4d82fa1ce12d4291c22d#file-gistfile1-txt. What am I doing wrong? – dmz73 Sep 22 '14 at 08:23
  • 1
    @dmz73 I misunderstood where the `NullPointerException` was coming from in your question. This seems like a bug. I've updated my answer accordingly. – Sotirios Delimanolis Sep 22 '14 at 17:41

1 Answers1

1

After review of what you meant and your comment in the question, this is very likely a bug in their implementation. The javadoc for ValueReader claims

Returns all member names for the source object, else null if the source has no members.

However, the only code that uses this method, PropertyInfoSetResolver#resolveAccessors(...), does not check for the existence of null. Member names in JSON only make sense for objects, but, here, you have a JSON array. That's why it fails.

As far as I can tell, the code doesn't check for null nor for source types that don't have members. I consider this a bug. The error is easily reproducible from the sample example by replacing any of the fields (and the corresponding JSON) with array types. You might want to contact the developer or change libraries.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Thanks. This seems to be the cause. I noticed this when I traced the execution of the library. Hopefully the author will fix this small bug. – dmz73 Sep 23 '14 at 06:43