JsonMappingException (was java.lang.NullPointerException)

40.8k views Asked by At

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?

5

There are 5 answers

2
Maxime T On BEST ANSWER

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

1
shane On

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.

0
Haranath Boggarapu On

I got the same issue after upgrading to spring boot 2.4.1 from 1.5, issue is one of the field is not getting serialized while invoking a GET Request. Earlier the piece of code looks like below (before spring boot upgrade)

@Bean
    public RepositoryRestConfigurer getRestConfigurer() {
        return new RepositoryRestConfigurerAdapter() {
            @Override
            void configureJacksonObjectMapper(ObjectMapper objectMapper) {
                super.configureJacksonObjectMapper(objectMapper)
                def validationResponseSerializerUtil = new ValidationResponseSerializerUtil()
                def simpleModule = validationResponseSerializerUtil.getSerializeModule()
                objectMapper.registerModule(simpleModule)
                objectMapper.enable(SerializationFeature.INDENT_OUTPUT)
            }
        }
    }

But after spring boot upgrade, above piece of code (serializing the ValidationResponse via RepositoryRestConfigurer) is not working, replaced it with below

@Bean(name = "objectMapper")
    ObjectMapper objectMapper() {
        def validationResponseSerializerUtil = new ValidationResponseSerializerUtil()
        def simpleModule = validationResponseSerializerUtil.getSerializeModule()
        ObjectMapper objectMapper=new ObjectMapper()
        objectMapper.registerModule(simpleModule)
        objectMapper.enable(SerializationFeature.INDENT_OUTPUT)
        return objectMapper
    }

Now everything works fine

0
Nishant Shekhar On

Always check before conversion:-

if(node.get("id")!=null) { int id = (Integer) ((IntNode)node.get("id")).numberValue();}

It may be possible that your json may not contain any key. Then it will throw null pointer exception. So better to check for null first.

0
thebiggestlebowski On

I ran into a NullPointerException on a class where I had tagged the constructors with @com.fasterxml.jackson.annotation.JsonCreator. When I removed the tag, deserialization json -> mojo worked.