question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NPE with `CollectionDeserializer` with `TreeSet` class, with input `null`

See original GitHub issue

Hello,

I’ve a problem while trying deserialize a TreeSet with a custom deserializer, in my situation deserializer not find the registry to add the collection, and returns null, but as TreeSet does not accept nulls, pops exception in Java, as below:

com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.framework.entity.dynamic.DocumentEntityInfo["folders"]->java.util.TreeSet[0])
    at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:378)
    at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:350)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:298)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:249)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:26)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:490)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:95)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:260)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:163)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:135)
    at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:120)
    at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:91)
    at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromAny(AsPropertyTypeDeserializer.java:163)
    at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:42)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3807)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2797)
Caused by: java.lang.NullPointerException
    at java.util.TreeMap.compare(TreeMap.java:1290)
    at java.util.TreeMap.put(TreeMap.java:538)
    at java.util.TreeSet.add(TreeSet.java:255)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:284)

The suggestion is to treat null for this situation in the Collection Deserializer, and not add the item in the list in return. Line 284 of CollectionDeserializer class.

https://github.com/FasterXML/jackson-databind/blob/master/src/main/java/com/fasterxml/jackson/databind/deser/std/CollectionDeserializer.java

I’ve searched for an issue linked to that, without success in the issue tracker.

Thanks for your help.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Oct 14, 2016

Actually while I do not like one-off feature for null handling for just collections, I think it would be valid to block null from TreeSet, given that it can not work with JDK.

0reactions
cowtowncodercommented, Nov 19, 2016

Actually closing as it should be handled via #1402, and I have no plans for type-specific hack.

Read more comments on GitHub >

github_iconTop Results From Across the Web

At first I insert null in TreeSet. After that I insert any other value ...
When you try to add null on empty TreeSet initially it does not contain any element to compare hence its add without NPE,...
Read more >
TreeSet in Java - GeeksforGeeks
For an empty tree-set, when trying to insert null as the first value, one will get NPE from JDK 7. From JDK 7...
Read more >
TreeSet (Java Platform SE 7 ) - Oracle Help Center
A NavigableSet implementation based on a TreeMap . The elements are ordered using their natural ordering, or by a Comparator provided at set...
Read more >
TreeSet in Java - javatpoint
Hierarchy of TreeSet class, example of java tree set class. ... Java TreeSet class doesn't allow null elements. Java TreeSet class is non-synchronized....
Read more >
Can TreeSet have null values in Java? - Quora
TreeSet elements are keys in a TreeMap and it cannot have null as a key! ... So TreeSet 's add() throws NullPointerException if...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found