I have data class with default values.
data class Project(
val code: String,
val name: String,
val categories: List<String> = emptyList())
Java reflection fails to instantiate the class when some values are null. I'm getting exception
java.lang.IllegalArgumentException: Parameter specified as non-null is null: method Project.<init>, parameter categories
This is because java.lang.reflect.Constructor<T>.instantiateClass method expects arguments that are not null.
I have the type information, I have the constructor definition but I'm not sure how to call the constructor so that it uses the default values (The values are coming from the database and categories can be null). Is there any way to achieve this in Kotlin?
Using in Kotlin:
default values in constructors are taken only when the particular parameter of that constructor is not passed to it at all .That means it is done to achieve various combination of parameteraised constructor. for example,
takes default values when used as Bird(), Bird("dove") or Bird(gender ="female") .
so to solve your issue you have to add ? next to the categories parameter. like this,
and no need for emptyList() default. When you using emptyList as in your question you have to check for null and omit that parameter like this
That is when using this data class in another kotlin class .
Using in JAVA:
But if you want to use this data class in any java class then as @Hotkey said it is not supported by default , as kotlin supports this default parameter by using some methods under the hood.
So to make it compatible with java class you have to add @JvmOverloads annotation but not as @Hotkey said it have to annotated like this