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?

holla
  • 141
  • 1
  • 9
  • 1
    Instead of performing null check on data class, do that check for any data class property. if(request.containerType == null) { // do something } – Rajasekhar Feb 23 '21 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 Feb 23 '21 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 Feb 23 '21 at 15:08
  • Hi @barkatme that is the thing. ideally it should not be null. but if someone passes null value, how should we handle it? – holla Feb 23 '21 at 15:10
  • btw, code you've writen is ok if you add ? to type to make it nullable, also you can use request object for if it's not null like: request?.conteinerType - this will return conteinerType if request isn't null or null in other way.. additionaly if u want to do somthing when it's null look for use of elvis operator ( ?: ) – barkatme Feb 23 '21 at 15:12
  • @holla Doesn't matter if it's ugly, make a separate function or class for validating such fields because this way it is more efficient. However if you still want to check whole properties or data class for null check at once, it is clearly answered here. https://stackoverflow.com/a/12362257/7725103 – Rajasekhar Feb 23 '21 at 15:13
  • @holla, if you use this fun from kotlin code, you'll should make sure the value you're passing isn't null, otherwise it' won't compile. If it will be called from java, and receives null here, than I think app will crash with KotlinNPE becouse this function cannot take null as defenition said – barkatme Feb 23 '21 at 15:14
  • @barkatme Initially i had done request?.conteinerType but in my review comments they said we should not expect nullable object and should be validated in top of the fun and throw exception. Hence wanted to see if any other way to implement this :( – holla Feb 23 '21 at 15:20
  • @Rajasekhar i looked at that solution. do you think if using for loop is efficient? – holla Feb 23 '21 at 15:23
  • 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 Feb 23 '21 at 15:24
  • 1
    @holla read this answer below, more the properties -> slows the performance. https://stackoverflow.com/a/16396361/7725103 – Rajasekhar Feb 23 '21 at 15:27
  • @Tenfour04 so you are saying if in case somebody passes null value for the data class from kotlin compilation will fail and if null value passed from java it will throw runtime exception?. if this is true - this is exactly what i want to solve! :). what is the best way to tackle this? Throw exception on top of fun? or something else? – holla Feb 23 '21 at 15:31
  • You've already achieved it with your code above. Just remove the `if` check for `null`. Java code will have thrown an exception before the body of your function is reached, so the value is already guaranteed to not be null in the body of your function. – Tenfour04 Feb 23 '21 at 15:43

2 Answers2

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 }
}
user2233706
  • 6,148
  • 5
  • 44
  • 86
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.

undermark5
  • 775
  • 6
  • 17