1

I have a data class (given below)

data class Request(val containerType: String,
                   val containerId: String)

which i’m calling as a param in another function like given below

fun someLogicalFunction(request: Request) {
    // validate if request is not null here
    if(request == null) { // i know this is wrong, what can be done for this?
        // do something
    } else { // do something }
}

How do check if request is not null directly in kotlin?

13
  • 1
    Instead of performing null check on data class, do that check for any data class property. if(request.containerType == null) { // do something }
    – Rajasekhar
    Commented Feb 23, 2021 at 15:05
  • Hi @Rajasekhar for now only 2 properties are there, say if there 5 such properties. Then will if not look messy to do like this f(request.containerType == null && request.containerId == null && request.anotherVariable == null && so on) is there a better way?
    – holla
    Commented Feb 23, 2021 at 15:08
  • 2
    if any property or function parameter can be null, then in their definition you should add '?' at the end of type like (request: Request?)
    – barkatme
    Commented Feb 23, 2021 at 15:08
  • 1
    "review comments" Is this from your workplace? Sounds like they may have some unconventional standards. If you don't want null to be passed, you typically just don't mark the properties as nullable with ?. The Kotlin compiler will prevent a nullable value from being passed, but if you pass null using Java code, it will throw an exception at runtime.
    – Tenfour04
    Commented Feb 23, 2021 at 15:24
  • 1
    @holla read this answer below, more the properties -> slows the performance. stackoverflow.com/a/16396361/7725103
    – Rajasekhar
    Commented Feb 23, 2021 at 15:27

2 Answers 2

1

The type of Request in the parameter is not nullable, so you can't pass Request as null. Change to nullable, and then it makes to sense to check whether it is null:

fun someLogicalFunction(request: Request?) {
    // validate if request is not null here
    if(request == null) { // i know this is wrong, what can be done for this?
        // do something
    } else { // do something }
}
1

The type of the parameter to someLogicalFunction is non-nullable (same with the properties of Request), so if it is being called from Kotlin code you have a compile time assurance that no null value will be given and a null check is unnecessary.

However, if you really want/need to allow nulls, I suggest something like this

data class Request(val containerType: String?,
                   val containerId: String?) {
    val isValid get() = containerId != null && containerType != null
}

fun someLogicalFunction(request: Request?) {
    request?.let{
        if (request.isValid) {
            val type = request.containerType!!
            val id = request.containerId!!
            // do something
            return //this will return out of someLogicalFunction
        }
    }
    //we will only execute code here if request was null or if we decided it was invalid
}

obviously replace the isValid implementation with something that makes sense for whatever you require.

Not the answer you're looking for? Browse other questions tagged or ask your own question.