JsonMappingException(是java.lang.NullPointerException) [英] JsonMappingException (was java.lang.NullPointerException)

查看:1103
本文介绍了JsonMappingException(是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格式存储的Adobe InDesign文档中提取信息。内容很容易读取并存储在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.

我遇到了2个问题:

1)对象树中充满了圆形的引用。我通过使用带有注释的混合来解决这个问题

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序列化程序,但这对NullPointerExcept没有帮助离子。

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 值,而不是a 值。只要值不是 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆