JsonMappingException(是 java.lang.NullPointerException)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12779278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JsonMappingException (was java.lang.NullPointerException)
提问by Steffen
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 Hymanson JSON.
我正在使用第三方库 ( IDMLlib) 从以 .idml 格式存储的 Adobe InDesign 文档中提取信息。内容很容易读取并存储在“Idml”类型的对象中,其中包含我需要的一切。现在,我想使用Hymanson JSON将此对象发送到 Web 客户端(浏览器)。
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
1) 对象树充满了循环引用。我已经通过使用带有注释的 Mix-ins 解决了这个问题
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")
So now I've got a lot of Mix-ins ready, if needed for problem 2.
所以现在我已经准备好了很多 Mix-ins,如果问题 2 需要的话。
2) I keep getting new object-specific errors when serializing.
2)我在序列化时不断收到新的特定于对象的错误。
--Output from testMethodsReturnsSomething--
| Failure: testMethods(package.IdmlServiceTests)
| com.fasterxml.Hymanson.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.Hymanson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:218)
at com.fasterxml.Hymanson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:183)
at com.fasterxml.Hymanson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:155)
at com.fasterxml.Hymanson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:533)
...
I've tried writing a custom NullValue/NullKey serializer, but that doesn't help the NullPointerException.
我尝试编写自定义 NullValue/NullKey 序列化程序,但这对 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?
是否有可以在我的 Mix-ins 中使用的注释来处理这个问题?
或者我还有另一种方法来序列化这个对象吗?
回答by Maxime T
You are right, Double
can handle nullvalue, 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, Hymanson was not able to serialize the nullvalue for long
.
你是对的,Double
可以处理空值,double
但不能。就我而言,我的属性属于类型,Long
但 getter 返回的是一个long
值而不是一个Long
值。只要值不为null ,这是可以接受的。但是当值为null 时,Hymanson 无法序列化 的null值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.
只需更改 getter 即可返回一个Long
而不是long
固定它。验证您的 getter 是否在您的情况下返回 aDouble
而不是 a double
。
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
ps:我知道这个问题已经很老了,但因为我遇到了同样的问题,而且这个问题在谷歌答案中排在第二位......似乎迟迟不回答它是公平的
回答by shane
I don't know if this is still valid but, I solved this problem by changing the version of com.fasterxml.Hymanson.dataformat:Hymanson-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.
我不知道这是否仍然有效,但是我通过更改com.fasterxml.Hymanson.dataformat:Hymanson-dataformat-yaml
. 早些时候我使用 2.3.0 并遇到同样的错误。我将其更改为 2.7.8,现在工作正常。