Mongo scala driver with case class get java.util.NoSuchElementException: key not found Exception
When the key doesn't exist, I get java.util.NoSuchElementException.
I have a case class:
case class Token(_id: ObjectId, token: String)
try reading data from ongodb:
private val tokenCodecProvider = Macros.createCodecProvider[Token]()
private val codecRegistry = fromRegistries( fromProviders(tokenCodecProvider), DEFAULT_CODEC_REGISTRY )
private val database = MongoClient().getDatabase("test").withCodecRegistry(codecRegistry)
val tokenTable: MongoCollection[Token] = database.getCollection("Token")
def test = tokenTable.find().subscribe(
(user: MobileDevice) => println(user),
(error: Throwable) => println(s"Query failed: ${error.getMessage}"),
() => println("Done")
)
I got an exception when token is empty in Mongodb
java.util.NoSuchElementException: key not found: token
at scala.collection.MapLike$class.default(MapLike.scala:228)
at scala.collection.AbstractMap.default(Map.scala:59)
at scala.collection.MapLike$class.apply(MapLike.scala:141)
at scala.collection.AbstractMap.apply(Map.scala:59)
at com.testmongo.Mongo$$anon$1$TokenMacroCodec$3.getInstance(mongo.scala:15)
at com.testmongo.Mongo$$anon$1$TokenMacroCodec$3.getInstance(mongo.scala:15)
at org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec$class.decode(MacroCodec.scala:102)
at com.testmongo.Mongo$$anon$1$TokenMacroCodec$3.decode(mongo.scala:15)
at com.mongodb.connection.ReplyMessage.<init>(ReplyMessage.java:57)
at com.mongodb.connection.QueryProtocol$QueryResultCallback.callCallback(QueryProtocol.java:535)
at com.mongodb.connection.ResponseCallback.onResult(ResponseCallback.java:48)
at com.mongodb.connection.ResponseCallback.onResult(ResponseCallback.java:23)
at com.mongodb.connection.DefaultConnectionPool$PooledConnection$2.onResult(DefaultConnectionPool.java:470)
at com.mongodb.connection.DefaultConnectionPool$PooledConnection$2.onResult(DefaultConnectionPool.java:464)
at com.mongodb.connection.UsageTrackingInternalConnection$3.onResult(UsageTrackingInternalConnection.java:119)
at com.mongodb.connection.UsageTrackingInternalConnection$3.onResult(UsageTrackingInternalConnection.java:115)
at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:49)
at com.mongodb.connection.InternalStreamConnection.executeCallbackAndReceiveResponse(InternalStreamConnection.java:378)
at com.mongodb.connection.InternalStreamConnection.access$1700(InternalStreamConnection.java:66)
at com.mongodb.connection.InternalStreamConnection$ResponseBuffersCallback.onResult(InternalStreamConnection.java:420)
at com.mongodb.connection.InternalStreamConnection$ResponseBuffersCallback.onResult(InternalStreamConnection.java:389)
at com.mongodb.connection.InternalStreamConnection$ResponseHeaderCallback.onSuccess(InternalStreamConnection.java:562)
at com.mongodb.connection.InternalStreamConnection$ResponseHeaderCallback.access$2200(InternalStreamConnection.java:517)
at com.mongodb.connection.InternalStreamConnection$ResponseHeaderCallback$ResponseBodyCallback.onResult(InternalStreamConnection.java:584)
at com.mongodb.connection.InternalStreamConnection$ResponseHeaderCallback$ResponseBodyCallback.onResult(InternalStreamConnection.java:568)
at com.mongodb.connection.InternalStreamConnection$3.completed(InternalStreamConnection.java:447)
at com.mongodb.connection.InternalStreamConnection$3.completed(InternalStreamConnection.java:444)
at com.mongodb.connection.AsynchronousSocketChannelStream$BasicCompletionHandler.completed(AsynchronousSocketChannelStream.java:218)
at com.mongodb.connection.AsynchronousSocketChannelStream$BasicCompletionHandler.completed(AsynchronousSocketChannelStream.java:201)
at sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:126)
at sun.nio.ch.Invoker.invokeDirect(Invoker.java:157)
at sun.nio.ch.UnixAsynchronousSocketChannelImpl.implRead(UnixAsynchronousSocketChannelImpl.java:553)
at sun.nio.ch.AsynchronousSocketChannelImpl.read(AsynchronousSocketChannelImpl.java:276)
at sun.nio.ch.AsynchronousSocketChannelImpl.read(AsynchronousSocketChannelImpl.java:297)
at com.mongodb.connection.AsynchronousSocketChannelStream.readAsync(AsynchronousSocketChannelStream.java:125)
at com.mongodb.connection.InternalStreamConnection.readAsync(InternalStreamConnection.java:444)
at com.mongodb.connection.InternalStreamConnection.access$2000(InternalStreamConnection.java:66)
at com.mongodb.connection.InternalStreamConnection$ResponseHeaderCallback.onResult(InternalStreamConnection.java:541)
at com.mongodb.connection.InternalStreamConnection$ResponseHeaderCallback.onResult(InternalStreamConnection.java:517)
at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:49)
at com.mongodb.connection.InternalStreamConnection$3.completed(InternalStreamConnection.java:447)
at com.mongodb.connection.InternalStreamConnection$3.completed(InternalStreamConnection.java:444)
at com.mongodb.connection.AsynchronousSocketChannelStream$BasicCompletionHandler.completed(AsynchronousSocketChannelStream.java:218)
at com.mongodb.connection.AsynchronousSocketChannelStream$BasicCompletionHandler.completed(AsynchronousSocketChannelStream.java:201)
at sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:126)
at sun.nio.ch.UnixAsynchronousSocketChannelImpl.finishRead(UnixAsynchronousSocketChannelImpl.java:430)
at sun.nio.ch.UnixAsynchronousSocketChannelImpl.finish(UnixAsynchronousSocketChannelImpl.java:191)
at sun.nio.ch.UnixAsynchronousSocketChannelImpl.onEvent(UnixAsynchronousSocketChannelImpl.java:213)
at sun.nio.ch.KQueuePort$EventHandlerTask.run(KQueuePort.java:301)
at sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
How do I make the marker field optional or provide a default value?
Many thanks.
+3
source to share
No one has answered this question yet
Check out similar questions: