Home » Java » Exception on Lambda.extract : “object is not an instance of declaring class”

Exception on Lambda.extract : “object is not an instance of declaring class”

Posted by: admin October 22, 2018 Leave a comment

Questions:

I’ve got this example:

Set<Entity> entities = repository.findById(id);
Set<String> stringSet = new HashSet<String>(Lambda.extract(entities, on(Entity.class).getName()));

Debugging, I’ve realized that the findById returns an array of Objects (Ljava.lang.Object). So what kind of issue comes after? Since I have arrays of objects, the on(Entitiy.class).getName() returns an exception, because it’s iterating on objects and not on Entity’s.. The exception is:

Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class

How do I solve, stating that I’m NOT using EntityManager but a jpa repository with @Query annotation?

Answers: