argonaut-shapeless icon indicating copy to clipboard operation
argonaut-shapeless copied to clipboard

Encoding with an `implicit val` causes null pointer exception

Open mrdziuban opened this issue 7 years ago • 0 comments

I just found that defining a case class and an implicit EncodeJson for it causes a null pointer exception when trying to encode an instance of the case class using the implicit val. It works if the val is not implicit, and also if the implicit is taken in the method params. I'm using v1.2.0-M5 with argonaut v6.2. Here are three examples to illustrate:

Null pointer exception

import argonaut._
import argonaut.Argonaut._
import argonaut.ArgonautShapeless._

object ExampleNPE {
  case class Foo(x: String)
  implicit val ejFoo: EncodeJson[Foo] = EncodeJson.of[Foo]

  def encodeFoo(): Json = ejFoo(Foo("bar"))
}

ExampleNPE.encodeFoo() // => null pointer exception

Working with non-implicit val

import argonaut._
import argonaut.Argonaut._
import argonaut.ArgonautShapeless._

object ExampleWorking1 {
  case class Foo(x: String)
  val ejFoo: EncodeJson[Foo] = EncodeJson.of[Foo]

  def encodeFoo(): Json = ejFoo(Foo("bar"))
}

ExampleWorking1.encodeFoo() // => argonaut.Json = {"x":"bar"}

Working with implicit method param

import argonaut._
import argonaut.Argonaut._
import argonaut.ArgonautShapeless._

object ExampleWorking2 {
  case class Foo(x: String)

  def encodeFoo()(implicit ejFoo: EncodeJson[Foo]): Json = ejFoo(Foo("bar"))
}

ExampleWorking2.encodeFoo() // => argonaut.Json = {"x":"bar"}

mrdziuban avatar Apr 28 '17 23:04 mrdziuban