JsonMappingException(是 java.lang.NullPointerException) [英] JsonMappingException (was java.lang.NullPointerException)
问题描述
我一直在寻找这个问题,但没有找到任何答案,所以要么我遗漏了一些明显没有人写过的东西,要么我遇到了一个不寻常的问题.我希望这是第一个...
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...
我正在使用第三方库 (IDMLlib) 从存储在.idml 格式.内容很容易读取并存储在Idml"类型的对象中,其中包含我需要的一切.现在,我想使用 Jackson JSON 将此对象发送到 Web 客户端(浏览器).
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.
我遇到了两个问题:
1) 对象树充满了循环引用.我已经通过使用带有注释的 Mix-ins 修复了这个问题
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")
所以现在我已经准备好了很多 Mix-ins,如果问题 2 需要的话.
So now I've got a lot of Mix-ins ready, if needed for problem 2.
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)
...
我尝试编写自定义 NullValue/NullKey 序列化程序,但这对 NullPointerException 没有帮助.
I've tried writing a custom NullValue/NullKey serializer, but that doesn't help the NullPointerException.
是否有可以在我的 Mix-ins 中使用的注释来处理这个问题?
或者我还有其他方法可以序列化这个对象吗?
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?
推荐答案
你说得对,Double
可以处理null值,而double
不能.就我而言,我的属性属于 Long
类型,但 getter 返回的是 long
值而不是 Long
值.只要值不是null,这是可以接受的.但是当值为 null 时,jackson 无法序列化 long
的 null 值.
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
.
只需更改 getter 以返回 Long
而不是 long
即可修复它.验证您的 getter 是否在您的情况下返回 Double
而不是 double
.
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:我知道这个问题已经很老了,但因为我遇到了同样的问题,而且这个问题在谷歌答案中排在第二位......似乎迟迟不回答它是公平的
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
这篇关于JsonMappingException(是 java.lang.NullPointerException)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!