How to validate if a data class is not null

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) {
// do something
} else { // do something }
}

How do check if request is not null?

request cannot be null in someLogicalFunction, its type is Request and it isn’t nullable.

See Null safety—Kotlin

2 Likes

@fvasco So you are saying kotlin will fail compilation?. if say we have configured for kotlin to work along side java and somebody passed null value how do i handle this scenario? what check can i do to and throw exception?

Well the ide will prevent a java caller from passing a null value to a function that doesn’t expect nulls. However, if you do expect that nulls will be passed to your function, you have to mark that type with a question mark at the end. I.e. the correct version of your function with null-handling is this:

fun someLogicalFunction(request: Request?) { //Notice the ? right there.
    // validate if request is not null here
    if(request == null) {
        // do something
    } else { // do something }
}

Again, please refer to the Kotlin null safety docs: Null safety—Kotlin

1 Like

@kyay10 Got it, i’m not expecting the caller to pass a null value. its just an arbitrary case. Is there a way to explicitly add a validation for request instance created? for example if request.containerType only becomes null is there a way to throw exception if mandatory fields are missing?

pardon me as i just started learning kotlin :slight_smile:

1 Like

so you want to make request.containerType nullable right? As in your data class would now be this: data class Request(val containerType: String?, val containerId: String)?
Then you can use requireNotNull(request.containerType) at the start of your function which throws an exception if it is null. Alternatively, you can use request.containerType!! which throws an NPE If it is null. The good thing about requireNotNull is that you can attach a message at the end like this requireNotNull(request.containerType) { "the container type must not be null" } but other than that both are practically similar

1 Like

Thank you for your reply @kyay10 i’m sorry if my previous reply was not that clear

i dont want the data class Request or its attributes to be null at all, i went through the Null safety—Kotlin. i just want to throw exception if any attribute of data class or the Request itself becomes null.

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

That automatically happens then because Kotlin under the hood ensures that even if a Java caller passes a null value that an exception will be thrown automatically so you don’t need to worry about that at all. Basically Kotlin automatically adds null checks at every one of these parameters and throws an NPE if an unexpected null was passed in.

1 Like

@kyay10 Uff finally getting more clarity. This was an amazing thread. So instead of NPE if i want to throw a custom exception if something went null, how do we achieve that in kotlin?

1 Like

Well, if you do want that then you have to use a nullable type and then throw your exception if the value is null like this:

fun someLogicalFunction(request: Request?) {
   request ?: throw MyCustomNullException("incorrect value for request") // The ?: is called the elvis operator and basically it performs the expression on the right hand side if the left hand side is null
   // From here on Kotlin understands that request cannot be null and so u can use it safely
   println(request.containerType) // This call is 100% safe
}

Sadly there’s no way to throw a custom exception if your type is not nullable (i.e. with a “?”) but in practice that really isn’t needed because IDE warnings tell you if you ever try to pass in a null value from Java to Kotlin

1 Like

I think a good question to ask is: “Why would any of these values be set to null?”

If your answer is: “Because that is an expected behavior” (even if that behavior is an expected failure), then those attributes should be typed as nullable.

3 Likes