NullPointerException during JSON deserialization
See original GitHub issueI’m seeing this very hard to reproduce NullPointerException when I’m trying to deserialize JSON with play-json into an enumeratum enum.
java.lang.NullPointerException: null
at enumeratum.Enum$$anonfun$lowerCaseNamesToValuesMap$1.apply(Enum.scala:47)
at enumeratum.Enum$$anonfun$lowerCaseNamesToValuesMap$1.apply(Enum.scala:47)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:234)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:234)
at scala.collection.Iterator$class.foreach(Iterator.scala:893)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1336)
at scala.collection.IterableLike$class.foreach(IterableLike.scala:72)
at scala.collection.AbstractIterable.foreach(Iterable.scala:54)
at scala.collection.TraversableLike$class.map(TraversableLike.scala:234)
at scala.collection.AbstractTraversable.map(Traversable.scala:104)
at enumeratum.Enum$class.lowerCaseNamesToValuesMap(Enum.scala:47)
It appears the the value of lowerCaseNamesToValuesMap
is null. My guess is that it is connected to the variable being declared lazy
.
If I access the variable lowerCaseNamesToValuesMap
before the deserilisation the problem goes away. I used to only see this in test code but now also in production code.
Have you seen anything like this before?
Issue Analytics
- State:
- Created 7 years ago
- Comments:12 (4 by maintainers)
Top Results From Across the Web
Getting NullPointerException while trying to deserialize json
The JSON returned contains "statewise" property. So to get it deserialized correctly you can either change the property List<Statewise> ...
Read more >Json deserialize System.NullPointerException: Attempt to de ...
You're missing the part where you get "Accts" and operate on that resource. Map<String, Object> fieldMap = (Map<String, Object>)JSON.
Read more >NullPointerException during deserialization happens recently
Please note this happens from time to time. It seems related to the authentication token. Everytime I re-generate the token and the error...
Read more >NullPointerException on a simple collection - Google Groups
JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java: 56) 07-19 20:16:34.823: ERROR/AndroidRuntime(285): at com.google.gson.
Read more >3 ways to ignore null fields while converting Java object to ...
Ignoring null fields or attribute is a one of the common requirement while marshaling Java object to JSON string because Jackson just prints...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Encountered this. The problem was triggered under a large ScalaTest suite.
It seems the scenario includes two threads racing on the initialization of SomeEnum$, SomeEnum$.values, and the various instances of SomeEnum$.Foo$, SomeEnum$.Bar$, SomeEnum$.Etc$. The problem is that when the various Foo/Bar/Etc values of SomeEnum are initialized by a different thread, the
findValues
macro might not see the value of Foo$.MODULE$ and accidentally use a null instead.Happened under scala 2.12.8 on two distinct multicore Intel processors (i7-7700HQ which is 4C8T, and another of a similar vintage which is 2C4T).
Not sure whether the “lazy val” causes a proper read barrier to be in place to guarantee the visibility of the other thread’s initialization of SomeEnum$.Foo$, or whether that invariant would hold in scala 2.13.0 (not in position to test/reproduce under 2.13 at this moment) or whether “lazy val” just causes enough delay that it now works by luck, but so far going with this.
Suggestion: the implementation of the findValues might want to strive to ensure the appropriate read barrier is in place before attempting to read each of the values’ respective MODULE$ static variables. Possibly, this happens in https://github.com/lloydmeta/enumeratum/blob/master/macros/src/main/scala/enumeratum/EnumMacros.scala#L160 but might require a JVM-specific hack to locate the appropriate thing to synchronize on?
The problem I think is strictly coupled with supplemental params added in a enum class and the macro applyed with
findValues
. I solved the issue declaring lazy findValue method.