Kotlin Realm: Class must declare a public constructor with no arguments if it contains custom constructors
Asked Answered
P

6

20

I'm creating a Realm object in Kotlin.

Realm Object:

open class PurposeModel(var _id: Long?,
                        var purposeEn: String?,
                        var purposeAr: String?) : RealmObject()

When I compile the above code I'm getting this error:

error: Class "PurposeModel" must declare a public constructor with no arguments if it contains custom constructors.

I can't find any question related to this in Kotlin. How do I resolve this?

Paronymous answered 8/3, 2018 at 7:44 Comment(0)
P
51

To clear this error you have to assign default values to properties.

Change the Realm Object like this:

open class PurposeModel(
    var _id: Long? = 0,
    var purposeEn: String? = null,
    var purposeAr: String? = null
) : RealmObject()

Now it will compile.

Reason:

When the default value not assigned it will become the parameters of the constructor, Realm need a public constructor with no arguments. When the default value assigned, it will become the properties of the class. So you will get empty constructor by default and clean code.

Paronymous answered 8/3, 2018 at 7:44 Comment(3)
Good one here; helpedKerakerala
This does not work in new version of the Realm Kotlin SDKDespiteful
@AbolfazlAbbasi the same. Did You find the solution? I am on Realm Kotlin SDK 1.13.0.Disserve
M
7

That's why I prefer to define them like this

open class PurposeModel : RealmObject() {
    @field:PrimaryKey
    var id: Long? = null
    var purposeEn: String? = null
    var purposeAr: String? = null
}
Maldives answered 8/3, 2018 at 12:16 Comment(0)
V
2

I use Realm on my Ktor server and you need to provide an empty constructor with default values.

@Serializable
class User(
    val id: String,
    var email: String,
    var hashedPass: ByteArray,
) : RealmObject {
    constructor() : this(
        id = ObjectId.create().toString(),
        email = "",
        hashedPass = byteArrayOf()
    ) // Empty constructor for Realm
}

Without it, I was getting and error:

[Realm] Cannot find primary zero arg constructor
Valenta answered 30/8, 2022 at 16:34 Comment(0)
I
0

Error:

_id, purposeEn, purposeAr not initialized

Solution

open class PurposeModel(
var _id: Long? = 0,
var purposeEn: String? = "",
var purposeAr: String? = ""
) : RealmObject()
Increment answered 22/3, 2018 at 3:4 Comment(0)
S
0

For me the solution was adding the abstract keyword, below is my working code,

@Log4j2
@RunWith(MockitoJUnitRunner::class)
@ExtendWith(MockitoExtension::class)
internal abstract class ItemsManagementControllerTest(
    private val logger: Logger = LoggerFactory.getLogger(
        ItemsManagementControllerTest::class.java
    ) as Logger,
    private var mvc: MockMvc? = null,
    @InjectMocks private val itemsManagementController: ItemsManagementController? = null,
    @Mock private val itemRepository: ItemRepository? = null
) {

Removing the abstract keywords fails the test cases in the class. I honestly do not understand why at the moment.

Siamang answered 13/5, 2023 at 18:58 Comment(0)
C
-1

You can also use the Kotlin no-arg plugin which generates a no-arg constructor for you.

Calliper answered 8/3, 2018 at 11:13 Comment(2)
It seems that is not possible, see this answer, if it is possible, please, could you provide a more complete answer?Jolyn
Hi, @GabrielDeOliveiraRohden. I don't know how Realm works internally (why it needs noarg constructor). When writing an answer, I assumed it works similarly to Hibernate (which is JavaEE ORM), which constructs entities using a no-arg constructor. I used the no-arg plugin with Hibernate in my project and it worked well. Although, I haven't tried with Realm.Maes

© 2022 - 2025 — McMap. All rights reserved.