I'm trying to incorporate a very simple Mongo model for my app, but my initial attempts have been futile. I am getting stuck with a NullPointerException
and I've even tried wrapping my call with a Try
and end up with a Success(scala.concurrent.impl.Promise$DefaultPromise@1f02f0b3)
Here's the code
def token = Action.async(parse.json) { request =>
request.body.validate[Auth].map { creds =>
UserService.login(creds.username, creds.password).map {
case Some(context) =>
val accessToken = TokenResponse(Crypto.generateToken)
val model = Json.toJson(context)
val res = Try(collection.insert(model).map(_ => Ok(Json.toJson(accessToken))))
println(res.toString)
res.get
case _ => Future(BadRequest)
}.flatMap(identity)
} recoverTotal (request => Future(Unauthorized))
}
Here's the error (notice no reference to my application in the call stack):
play.api.Application$$anon$1: Execution exception[[NullPointerException: null]]
at play.api.Application$class.handleError(Application.scala:296) ~[play_2.11-2.3.6.jar:2.3.6]
at play.api.DefaultApplication.handleError(Application.scala:402) [play_2.11-2.3.6.jar:2.3.6]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.6.jar:2.3.6]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.6.jar:2.3.6]
at scala.Option.map(Option.scala:145) [scala-library-2.11.2.jar:na]
Caused by: java.lang.NullPointerException: null
at reactivemongo.bson.buffer.WritableBuffer$class.writeString(buffer.scala:71) ~[reactivemongo-bson_2.11-0.10.5.0.akka23.jar:0.10.5.0.akka23]
at reactivemongo.core.netty.ChannelBufferWritableBuffer.writeString(netty.scala:61) ~[reactivemongo_2.11-0.10.5.0.akka23.jar:0.10.5.0.akka23]
at reactivemongo.bson.buffer.DefaultBufferHandler$BSONStringBufferHandler$.write(bufferhandlers.scala:83) ~[reactivemongo-bson_2.11-0.10.5.0.akka23.jar:0.10.5.0.akka23]
at reactivemongo.bson.buffer.DefaultBufferHandler$BSONStringBufferHandler$.write(bufferhandlers.scala:82) ~[reactivemongo-bson_2.11-0.10.5.0.akka23.jar:0.10.5.0.akka23]
at reactivemongo.bson.buffer.DefaultBufferHandler$.serialize(bufferhandlers.scala:227) ~[reactivemongo-bson_2.11-0.10.5.0.akka23.jar:0.10.5.0.akka23]
I tried using Postman and ended up with this reference, but not much more helpful:
app/controllers/Application.scala:27
It ended up being simple...my resulting JSON had null values and ReactiveMongo didn't like that. I will be submitting a bug to gracefully fail or at least be more descriptive.