Reputation: 469
I've got a HashMap<ObjectId, Integer>
in which I want to add a new entry.
ObjectId
is a BSON
class which comes with the MongoDB-Driver
in my case.
1 @Expose private Map<ObjectId, Integer> usedPaths;
2
3 public Project()
4 {
5 this.usedPaths = new HashMap<>();
6 }
7
8 public void usePath(ObjectId someId, int pathId)
9 {
10 this.usedPaths.put(someId, pathId);
11 }
12
13 public void debug()
14 {
15 usePath(new ObjectId("5bfd98a2c06178796e66c204"), 1);
16 }
When I call the debug method, a ClassCastException
is thrown:
SCHWERWIEGEND: Servlet.service() for servlet [Jersey Web Application] in context with path [/KnowledgerServer] threw exception [java.lang.ClassCastException:
org.bson.types.ObjectId cannot be cast to java.lang.String] with root cause
java.lang.ClassCastException: org.bson.types.ObjectId cannot be cast to java.lang.String
at org.bson.BasicBSONObject.put(BasicBSONObject.java:36)
at de.knowledger.model.Project.usePath(Project.java:10)
at de.knowledger.model.Project.debug(Project.java:15)
As far as I know and see I don't even try to cast an ObjectId to String, furthermore I'm never accessing the BasicBSONObject
class in my code, ObjectId
doesn't extend it either.
Thanks in advance :)
Upvotes: 1
Views: 702
Reputation: 469
The problem was that I tried to save and load a HashMap to/from MongoDB, its Java driver handles any passed Map as DBObject.
Thank you for your Tips!
Upvotes: 0
Reputation: 244
There is no error in the piece of code you have provided, as far as I can see. The exception makes it clear that the class is part of a servlet. The issue could be in what is happening before and after you call the usePath(...)
method.
Upvotes: 1