Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException: null #227

Open
sm-tester opened this issue Apr 25, 2017 · 9 comments
Open

NullPointerException: null #227

sm-tester opened this issue Apr 25, 2017 · 9 comments

Comments

@sm-tester
Copy link

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

@sm-tester
Copy link
Author

sm-tester commented Apr 28, 2017 via email

@hveiga
Copy link

hveiga commented Apr 28, 2017

@Boyfox can you paste how you have your jsonFormat implicits?

@sm-tester
Copy link
Author

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"))

@leo-da
Copy link

leo-da commented May 2, 2017

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.

@sm-tester
Copy link
Author

ok, I fixed, thank you, you can close or delete this issue

@ktoso ktoso closed this as completed May 3, 2017
@leo-da
Copy link

leo-da commented May 3, 2017

Just curios... don't you guys want to fix this NPE?

@ktoso
Copy link
Member

ktoso commented May 3, 2017

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

@ktoso ktoso reopened this May 3, 2017
@leo-da
Copy link

leo-da commented May 3, 2017

sounds good. I will have a look. I was planning to raise the NPE issue, but found this thread.
Regards,
Leo

@amanjain08
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants