spray-json
spray-json copied to clipboard
NullPointerException: null
Hi, I have following a case class:
case class UserResp(
id: Int,
firstName: Option[String] = None,
lastName: Option[String] = None,
email: Option[String] = None,
avatar: Option[String] = None,
avatarW: Option[Int] = None,
avatarH: Option[Int] = None,
birthday: Option[String] = None,
gender: Option[String] = None,
phone: Option[String] = None,
height: Option[Int] = None,
place: Option[Place] = None,
playedCount: Option[Int] = None,
missedCount: Option[Int] = None,
hostedCount: Option[Int] = None,
evaluations: Option[Float] = None,
position: Option[GPositions] = None,
created: Option[Long] = None,
oAuthPlatform: Option[OAuthPlatform.OAuthPlatform] = None
)
this is Place case class:
case class Place(
id: Option[Int] = None,
title: Option[String] = None,
address: Option[String] = None,
latitude: Option[Float],
longitude: Option[Float],
city: Option[String] = None,
region: Option[String] = None
)
this is GPositions case class:
case class GPositions(id: Int, name: String)
Appears always following error on Runtime:
java.lang.NullPointerException: null
at spray.json.PimpedAny.toJson(package.scala:39)
at spray.json.StandardFormats$OptionFormat.write(StandardFormats.scala:34)
at spray.json.StandardFormats$OptionFormat.write(StandardFormats.scala:32)
at spray.json.ProductFormats$class.productElement2Field(ProductFormats.scala:46)
at spray.json.DefaultJsonProtocol$.productElement2Field(DefaultJsonProtocol.scala:30)
at spray.json.ProductFormatsInstances$$anon$19.write(ProductFormatsInstances.scala:707)
at spray.json.ProductFormatsInstances$$anon$19.write(ProductFormatsInstances.scala:692)
at spray.httpx.SprayJsonSupport$$anonfun$sprayJsonMarshaller$1.apply(SprayJsonSupport.scala:43)
at spray.httpx.SprayJsonSupport$$anonfun$sprayJsonMarshaller$1.apply(SprayJsonSupport.scala:42)
at spray.httpx.marshalling.Marshaller$MarshallerDelegation$$anonfun$apply$1.apply(Marshaller.scala:58)
at spray.httpx.marshalling.Marshaller$MarshallerDelegation$$anonfun$apply$1.apply(Marshaller.scala:58)
at spray.httpx.marshalling.Marshaller$MarshallerDelegation$$anonfun$apply$2.apply(Marshaller.scala:61)
at spray.httpx.marshalling.Marshaller$MarshallerDelegation$$anonfun$apply$2.apply(Marshaller.scala:60)
at spray.httpx.marshalling.Marshaller$$anon$2.apply(Marshaller.scala:47)
at spray.httpx.marshalling.BasicToResponseMarshallers$$anon$1.apply(BasicToResponseMarshallers.scala:35)
at spray.httpx.marshalling.BasicToResponseMarshallers$$anon$1.apply(BasicToResponseMarshallers.scala:22)
at spray.httpx.marshalling.ToResponseMarshaller$$anonfun$compose$1.apply(Marshaller.scala:69)
at spray.httpx.marshalling.ToResponseMarshaller$$anonfun$compose$1.apply(Marshaller.scala:69)
at spray.httpx.marshalling.ToResponseMarshaller$$anon$3.apply(Marshaller.scala:81)
at spray.httpx.marshalling.MetaToResponseMarshallers$$anonfun$futureMarshaller$1$$anonfun$apply$1.apply(MetaToResponseMarshallers.scala:44)
at spray.httpx.marshalling.MetaToResponseMarshallers$$anonfun$futureMarshaller$1$$anonfun$apply$1.apply(MetaToResponseMarshallers.scala:43)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
at akka.dispatch.BatchingExecutor$AbstractBatch.processBatch(BatchingExecutor.scala:55)
at akka.dispatch.BatchingExecutor$BlockableBatch$$anonfun$run$1.apply$mcV$sp(BatchingExecutor.scala:91)
at akka.dispatch.BatchingExecutor$BlockableBatch$$anonfun$run$1.apply(BatchingExecutor.scala:91)
at akka.dispatch.BatchingExecutor$BlockableBatch$$anonfun$run$1.apply(BatchingExecutor.scala:91)
at scala.concurrent.BlockContext$.withBlockContext(BlockContext.scala:72)
at akka.dispatch.BatchingExecutor$BlockableBatch.run(BatchingExecutor.scala:90)
at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:39)
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:409)
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
I don't understand why appears this error, I did not use recursive types
Hi, I'm using 2.11.8 and Spray-json 1.3.2
On Thu, Apr 27, 2017 at 8:40 PM, Hector Veiga [email protected] wrote:
We are seeing the same issue with Scala 2.12 Spray-Json 1.3.3. @boyfox https://github.com/boyfox which version are you using?
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/spray/spray-json/issues/227#issuecomment-297752819, or mute the thread https://github.com/notifications/unsubscribe-auth/ADBZtpid82yrAYSBGd1dtuS2YqiA9FcNks5r0Lb6gaJpZM4NG_0I .
@boyfox can you paste how you have your jsonFormat implicits?
Hi, here declared implicit: implicit val userRespFormat: JsonFormat[UserResp] = lazyFormat(jsonFormat(UserResp, "id", "firstName", "lastName", "email", "avatar", "avatarW", "avatarH", "birthday", "gender", "phone", "height", "place","playedCount", "missedCount", "hostedCount", "evaluations", "position", "created", "oAuthPlatform"))
You need to define implicit JsonFormatters for Place, GPositions and OAuthPlatform... that is for sure.
What I also noticed, please correct me if I am wrong... the order matters, Place, GPositions and OAuthPlatform formatters need to be defined before the UserResp formatter.
However, the NullPointerException is kind of bad and probably should be addressed.
ok, I fixed, thank you, you can close or delete this issue
Just curios... don't you guys want to fix this NPE?
I did not investigate deeply enough to know, let's re-open this however we're not actively working on it right now.
If you're interested a pull request would be really welcome
sounds good. I will have a look. I was planning to raise the NPE issue, but found this thread. Regards, Leo
For future readers: Check the order of implicit jsonformats. If class A has an implicit dependency on class B jsonformats, then class B's jsonformats should be defined before class A.