简体   繁体   中英

java.lang.NoSuchMethodException : package.ClassName.<init>() Using Class<?>

I am instantiating a new class using this code:

Class<?> clazz; Object object;

clazz = Class.forname(package.ClassName);//dynamic classes 

object = clazz.newInstance();

Now the problem is whenever I run this code, it gives me a NoSuchMethodExeception: package.ClassName.

ClassName has a parameter, and I can't remove it since it is a must.

I tried removing the parameter then the error gone (parameter must be always there for some reason).

Why is it working like this?

As per documentation , the function Class#newInstance is only capable of initializing classes that provide a no-arg constructor.

If you want to initialize a type which doesn't provide a no-arg constructor and therefore requires the constructor's caller to provide an argument, you can use the java.lang.reflect.Constructor-API .

Constructor<?> constr = clazz.getConstructor(classTypeOfArgument);
Object instance = constr.newInstance(paramInstance); 

Note: Using raw types is a rather bad idea in this context as it seems like you already know what kind of types you're dealing with.

@Shankar Saran Singh This has absolutely nothing to do with dynamic type creation at runtime and the code snippet you've provided doesn't solve the author's question at all.

As per my understanding your class has no default constructor. That's why you get this kind of exception. So You have to implement default constructor explicitly if you made any constructor in class. This exception comes from clazz.newInstance();

Well, I also have similar problem in kotlin JPA. When I use a JPAQueryFactory Projections.fields that needs DTO like below.

class AdCampaignDailyStatDTO @QueryProjection constructor(
var id: Long?,
var accountId: Long?,
var name: String?,
var type: AdCampaignType?,
var onOff: OnOffType?,
var status: AdCampaignStatus?,
var budgetLimit: BigDecimal?

but it occured 'java.lang.NoSuchMethodException' like you. Even though many sample code guides that.

com.querydsl.core.types.ExpressionException: com.navercorp.groupn.gpc.entity.stats.dto.AdCampaignDailyStatDTO] with root cause
java.lang.NoSuchMethodException: com.navercorp.groupn.gpc.entity.stats.dto.AdCampaignDailyStatDTO.<init>()
    at java.base/java.lang.Class.getConstructor0(Class.java:3349)
    at java.base/java.lang.Class.newInstance(Class.java:556)
    at com.querydsl.core.types.QBean.create(QBean.java:255)
    at com.querydsl.core.types.QBean.newInstance(QBean.java:222)

And I ended up solving problem like below. It means DTO code needs property initial value that has to define 'null' in my case. like this.

class AdCampaignDailyStatDTO @QueryProjection constructor(
    var id: Long? = null,
    var accountId: Long? = null,
    var name: String? = null,
    var type: AdCampaignType? = null,
    var onOff: OnOffType? = null,
    var status: AdCampaignStatus? = null,
    var budgetLimit: BigDecimal? = null
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM