hb0
hb0

Reputation: 3737

How to fix "class UUID cannot be cast to class String"

I'm storing a UUID in Mongo DB and when I want to read it again from MongoDB with

UUID.fromString(document.getString("userId"))

I get:

class java.util.UUID cannot be cast to class java.lang.String (java.util.UUID and java.lang.String are in module java.base of loader 'bootstrap')

java.lang.ClassCastException: class java.util.UUID cannot be cast to class java.lang.String 

But the Document class of the Java Mongo Driver has no .getUUID() method. How to fix this?

I was expecting to be able to read the UUID without errors.

Upvotes: 0

Views: 2596

Answers (1)

hb0
hb0

Reputation: 3737

The fix seems to be:

(UUID)document.get("userId")

Upvotes: 0

Related Questions