JsonMappingException (was java.lang.NullPointerExc

2020-02-12 06:43发布

问题:

I've been searching for this for a while but haven't found any answers, so either I'm missing something so obvious noone has written anything about it, or I've hit an unusual problem. I'm hoping it's the first...

I'm working with a third-party library (IDMLlib) to extract information from an Adobe InDesign document stored in the .idml format. The contents are easily read and stored in an object of type "Idml", which contains everything I need. Now, I want to send this object to a web client (browser) using Jackson JSON.

I've hit 2 problems:

1) The object tree is full of circular referefences. I've fixed this by using Mix-ins with the annotation

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")  

So now I've got a lot of Mix-ins ready, if needed for problem 2.

2) I keep getting new object-specific errors when serializing.

--Output from testMethodsReturnsSomething--
| Failure:  testMethods(package.IdmlServiceTests)
|  com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: de.fhcon.idmllib.api.elements.Idml["document"]->de.fhcon.idmllib.api.elements.Document["tags"]->de.fhcon.idmllib.api.elements.tags.Tags["xmltagList"]->java.util.ArrayList[0]->de.fhcon.idmllib.api.elements.tags.XMLTag["tagColor"]->de.fhcon.idmllib.api.elements.typedefs.InDesignUIColorType["greenValue"])
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:218)
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:183)
at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:155)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:533)
...

I've tried writing a custom NullValue/NullKey serializer, but that doesn't help the NullPointerException.

Is there an annotation I can use in my Mix-ins that handles this?
Or is there another way for me to serialize this object?

回答1:

You are right, Double can handle null value, and double can't. In my case, my property was of type Long but the getter was returning a long value and not a Long value. This was acceptable as far as the value was not null. But when the value was null, jackson was not able to serialize the null value for long.

Just changing the getter to return a Long instead of a long fixed it. Verify if your getter return a Double and not a double in your case.

ps: I know this question is quite old but since I was having the same problem and the question was 2nd in Google answers... Seemed fair to answer it later than never



回答2:

I don't know if this is still valid but, I solved this problem by changing the version of com.fasterxml.jackson.dataformat:jackson-dataformat-yaml. Earlier I was using 2.3.0 and was getting this same error. I changed it to 2.7.8 and it is working fine now.