Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 378 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Kotlin Realm: Class must declare a public constructor with no arguments if it contains custom constructors

#1
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?

Reply

#2
You can also use the [Kotlin no-arg plugin][1] which generates a no-arg constructor for you.


[1]:

[To see links please register here]

Reply

#3
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
}
Reply

#4

**Error:**

_id, purposeEn, purposeAr not initialized

**Solution**

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

#5
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.
Reply

#6
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
Reply

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

```kotlin
@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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through