java.lang.InstantiationException

There are no available Samebug tips for this exception. Do you have an idea how to solve this issue? A short tip would help users who saw this issue last week.

  • Note: Due to Eclipse debugging weirdness, I haven't been able to trace exactly where this problem goes wrong, but it appears to be around {{MappingMongoConverter:200}} when figuring out {{typeToUse}}. I have a hierarchy of MongoDB document types that inherit from an abstract base class and are saved in a single collection. The subclasses use {{@TypeAlias}} to distinguish between them. If the subclass defines any extra fields in addition to the super's, the mapping works properly, and the runtime type retrieved from the repository is the correct one. However, if the subclass does not define any fields beyond the ones on the superclass, SDM looks up the {{_class}} property on the document but then does not use it to look up the correct child class. Instead, it attempts to invoke the abstract superclass's no-arg constructor, which throws an {{InstantiationException}}. I can't provide a simple example that will run without a live MongoDB database, but it appears that the {{BasicDBObject}} being mapped is completely correct, and the problem is specifically with the selection of the {{PreferredConstructor}}; by the time the mapper gets to the {{createInstance}} call at 249, it's selected the invalid superclass constructor. {code:title=stack trace from the find invocation on the repository interface} java.lang.InstantiationException: null at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147) at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:76) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:249) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:230) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:190) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:186) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:77) at org.springframework.data.mongodb.core.MongoTemplate$ReadDbObjectCallback.doWith(MongoTemplate.java:2121) at org.springframework.data.mongodb.core.MongoTemplate.executeFindOneInternal(MongoTemplate.java:1760) at org.springframework.data.mongodb.core.MongoTemplate.doFindOne(MongoTemplate.java:1577) at org.springframework.data.mongodb.core.MongoTemplate.findOne(MongoTemplate.java:497) at org.springframework.data.mongodb.repository.query.AbstractMongoQuery$SingleEntityExecution.execute(AbstractMongoQuery.java:307) at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.execute(AbstractMongoQuery.java:107) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:421) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:381) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) at com.sun.proxy.$Proxy101.findByKey(Unknown Source) {code}
    via by Christopher Smith,
  • Note: Due to Eclipse debugging weirdness, I haven't been able to trace exactly where this problem goes wrong, but it appears to be around {{MappingMongoConverter:200}} when figuring out {{typeToUse}}. I have a hierarchy of MongoDB document types that inherit from an abstract base class and are saved in a single collection. The subclasses use {{@TypeAlias}} to distinguish between them. If the subclass defines any extra fields in addition to the super's, the mapping works properly, and the runtime type retrieved from the repository is the correct one. However, if the subclass does not define any fields beyond the ones on the superclass, SDM looks up the {{_class}} property on the document but then does not use it to look up the correct child class. Instead, it attempts to invoke the abstract superclass's no-arg constructor, which throws an {{InstantiationException}}. I can't provide a simple example that will run without a live MongoDB database, but it appears that the {{BasicDBObject}} being mapped is completely correct, and the problem is specifically with the selection of the {{PreferredConstructor}}; by the time the mapper gets to the {{createInstance}} call at 249, it's selected the invalid superclass constructor. {code:title=stack trace from the find invocation on the repository interface} java.lang.InstantiationException: null at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147) at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:76) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:249) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:230) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:190) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:186) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:77) at org.springframework.data.mongodb.core.MongoTemplate$ReadDbObjectCallback.doWith(MongoTemplate.java:2121) at org.springframework.data.mongodb.core.MongoTemplate.executeFindOneInternal(MongoTemplate.java:1760) at org.springframework.data.mongodb.core.MongoTemplate.doFindOne(MongoTemplate.java:1577) at org.springframework.data.mongodb.core.MongoTemplate.findOne(MongoTemplate.java:497) at org.springframework.data.mongodb.repository.query.AbstractMongoQuery$SingleEntityExecution.execute(AbstractMongoQuery.java:307) at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.execute(AbstractMongoQuery.java:107) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:421) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:381) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) at com.sun.proxy.$Proxy101.findByKey(Unknown Source) {code}
    via by Christopher Smith,
  • GitHub comment 63#277495093
    via GitHub by joswr1ght
    ,
    • java.lang.InstantiationException: null at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147) at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:76) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:249) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:230) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:190) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:186) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:77) at org.springframework.data.mongodb.core.MongoTemplate$ReadDbObjectCallback.doWith(MongoTemplate.java:2121) at org.springframework.data.mongodb.core.MongoTemplate.executeFindOneInternal(MongoTemplate.java:1760) at org.springframework.data.mongodb.core.MongoTemplate.doFindOne(MongoTemplate.java:1577) at org.springframework.data.mongodb.core.MongoTemplate.findOne(MongoTemplate.java:497) at org.springframework.data.mongodb.repository.query.AbstractMongoQuery$SingleEntityExecution.execute(AbstractMongoQuery.java:307) at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.execute(AbstractMongoQuery.java:107) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:421) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:381) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) at com.sun.proxy.$Proxy101.findByKey(Unknown Source)

    Users with the same issue

    Unknown visitor1 times, last one,
    Unknown visitor1 times, last one,
    Unknown visitor1 times, last one,
    Unknown visitor1 times, last one,
    filpgame
    6 times, last one,
    17 more bugmates