question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

java.lang.InstantiationException when trying to find a class that extends a abstract class.

See original GitHub issue

I created something like this below:

@Entity public abstract class myabs { }

public class child extends myabs { }

when I tried to find using the code like below:

new AnonymousClass { public void find() {

Mongo mongo ...
Morphia mor ...
...                      

List<ObjectId>ids = dao.findIds();
Model model = null;
for(ObjectId id : ids)
  model = dao.get(id);

} }

I got the exception:

java.lang.RuntimeException: java.lang.RuntimeException at org.mongodb.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:161) at org.mongodb.morphia.mapping.Mapper.readMappedField(Mapper.java:604) at org.mongodb.morphia.mapping.Mapper.fromDb(Mapper.java:584) at org.mongodb.morphia.mapping.Mapper.fromDBObject(Mapper.java:294) at org.mongodb.morphia.query.MorphiaIterator.convertItem(MorphiaIterator.java:71) at org.mongodb.morphia.query.MorphiaIterator.processItem(MorphiaIterator.java:58) at org.mongodb.morphia.query.MorphiaIterator.next(MorphiaIterator.java:53) at org.mongodb.morphia.query.QueryImpl.get(QueryImpl.java:408) at org.mongodb.morphia.DatastoreImpl.get(DatastoreImpl.java:606) at org.mongodb.morphia.dao.BasicDAO.get(BasicDAO.java:165) at controllers.CityController$1$1.invoke(CityController.java:77) at controllers.CityController$1$1.invoke(CityController.java:37) at play.core.j.JavaWebSocket$$anonfun$webSocketWrapper$1$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$3.apply(JavaWebSocket.scala:43) at play.core.j.JavaWebSocket$$anonfun$webSocketWrapper$1$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$3.apply(JavaWebSocket.scala:43) at scala.collection.Iterator$class.foreach(Iterator.scala:727) at scala.collection.AbstractIterator.foreach(Iterator.scala:1157) at scala.collection.IterableLike$class.foreach(IterableLike.scala:72) at scala.collection.AbstractIterable.foreach(Iterable.scala:54) at play.core.j.JavaWebSocket$$anonfun$webSocketWrapper$1$$anonfun$apply$1$$anonfun$apply$2.apply(JavaWebSocket.scala:43) at play.core.j.JavaWebSocket$$anonfun$webSocketWrapper$1$$anonfun$apply$1$$anonfun$apply$2.apply(JavaWebSocket.scala:43) at play.api.libs.iteratee.Iteratee$$anonfun$foreach$1.apply(Iteratee.scala:198) at play.api.libs.iteratee.Iteratee$$anonfun$foreach$1.apply(Iteratee.scala:198) at play.api.libs.iteratee.Iteratee$$anonfun$fold$1$$anonfun$apply$1.apply(Iteratee.scala:38) at play.api.libs.iteratee.internal$.eagerFuture(package.scala:27) at play.api.libs.iteratee.Iteratee$$anonfun$fold$1.apply(Iteratee.scala:38) at play.api.libs.iteratee.Iteratee$$anonfun$fold$1.apply(Iteratee.scala:38) at play.api.libs.iteratee.Iteratee$$anonfun$1.apply(Iteratee.scala:57) at play.api.libs.iteratee.Iteratee$$anonfun$1.apply(Iteratee.scala:57) at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24) at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) at scala.concurrent.forkjoin.ForkJoinTask$AdaptedRunnableAction.exec(ForkJoinTask.java:1361) at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) Caused by: java.lang.RuntimeException at org.mongodb.morphia.mapping.DefaultCreator.createInst(DefaultCreator.java:127) at org.mongodb.morphia.mapping.DefaultCreator.createInstance(DefaultCreator.java:31) at org.mongodb.morphia.mapping.DefaultCreator.createInstance(DefaultCreator.java:42) at org.mongodb.morphia.mapping.DefaultCreator.createInstance(DefaultCreator.java:54) at org.mongodb.morphia.mapping.EmbeddedMapper.readMapOrCollectionOrEntity(EmbeddedMapper.java:246) at org.mongodb.morphia.mapping.EmbeddedMapper.readCollection(EmbeddedMapper.java:191) at org.mongodb.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:135) … 34 more Caused by: java.lang.InstantiationException at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:30) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at org.mongodb.morphia.mapping.DefaultCreator.createInst(DefaultCreator.java:125) … 40 more

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
evanchoolycommented, Nov 15, 2013

I’m assuming this worked for you. I’ll close this in a few days if I haven’t heard back.

0reactions
evanchoolycommented, Oct 16, 2013

That abstract class is going to break things because it can’t be instantiated so it needs to be not abstract. To fix your queries to correctly return the Child class do something like this:

datastore.createQuery(Child.class).filter("className = ", Child.class.getName()) …

That should resolve both issues for you.

On Tue, Oct 15, 2013 at 8:25 PM, Abraão Isvi notifications@github.comwrote:

Here my code below (simplified and with some changes), I did not have time to write another code, but maybe friday I can try it.

Recently I changed the abstract class to not abstract but that created another problem, now when I fetch the model it brings me the father class, not instantiating the child classes.

For example:

public abstract class Father { … }

public class Child extends Father { … }

public class Model { List items; … }

Where the error occurs:

public class MyController extends Controller{

public static WebSocket<JsonNode> map() { return new WebSocket<JsonNode>() {

    public void onReady(WebSocket.In<JsonNode> in, final WebSocket.Out<JsonNode> out) {

        // For each event received on the socket,
        in.onMessage(new Callback<JsonNode>() {

            @Override
            public void invoke(JsonNode message) throws Throwable {
                try {

                    String name = message.get("name").asText();
                    if(name.equalsIgnoreCase(MyConstants.SHOW_MAP_MESSAGE_NAME)) {
                        getModel(message, out);
                    }
                } catch (Exception e) {
                    e.printStackTrace();

                    ObjectMapper mapper = new ObjectMapper();
                    out.write(mapper.valueToTree("erro"));
                }

            }
        });

        // When the socket is closed.
        in.onClose(new Callback0() {

            @Override
            public void invoke() throws Throwable {
                // TODO Auto-generated method stub

            }
        });

    }
  };

}

private static void getModel(JsonNode message, final WebSocket.Out<JsonNode> out) throws Exception {

Mongo mgc = UtilRepository.getMongoClient();
Morphia mor = UtilRepository.getMorphia();

ModelRepository repo = new ModelRepository(mgc, mor);

List<ObjectId>ids = repo.findIds();
Model model = null;
for(ObjectId id : ids) {
     model = repo.get(id);
}

ObjectMapper mapper = new ObjectMapper();
out.write(mapper.valueToTree(model));

}

}

I hope this helps.

— Reply to this email directly or view it on GitHubhttps://github.com/mongodb/morphia/issues/538#issuecomment-26384131 .

Read more comments on GitHub >

github_iconTop Results From Across the Web

java.lang.InstantiationException with abstract class Using ...
Spring tries to create singleton beans for the annotated classes. In your case you should have annotated abstract class.
Read more >
Can We Instantiate an Abstract Class in Java? - GeeksforGeeks
Abstract class, we have heard that abstract class are classes which can have abstract methods and it can't be instantiated.
Read more >
How to Resolve the Instantiation Exception in Java - Rollbar
The InstantiationException in Java is thrown when the JVM cannot instantiate a Java type at runtime using the Class.newInstance() method.
Read more >
java.lang Class InstantiationException
public class InstantiationException; extends Exception. Thrown when an application tries to create an instance of a class using the newInstance method in ...
Read more >
Abstract Class in Java | DigitalOcean
An abstract class is mostly used to provide a base for subclasses to extend and implement the abstract methods and override or use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found