user1743524
user1743524

Reputation: 715

Kotlin null pointer when checking for null state

I have a codeblock with a simple null check that causes an exception

kotlin.UninitializedPropertyAccessException: lateinit property currentJob has not been initialized at com.plcoding.posterpalfeature.ui.MainViewModel.getCurrentJob(MainViewModel.kt:40) at com.plcoding.posterpalfeature.ui.MainViewModel.getJobsListFromApi(MainViewModel.kt:284)

I've tried this -->

 with(currentJob){
            if (this == null) {
                currentJob = jobsDataList.get(0) //maybe replace with accepted jobs depending on how details is implemented
            }
            else {
                //update the current job using job id to get fresh object
                currentJob = jobsDataList.filter {
                    it.job.job_id == currentJob.job.job_id
                }.get(0)
            }//should only ever be single object
        }

this

if (currentJob == null) {
                currentJob = jobsDataList.get(0) //maybe replace with accepted jobs depending on how details is implemented
            }
            else {
                //update the current job using job id to get fresh object
                currentJob = jobsDataList.filter {
                    it.job.job_id == currentJob.job.job_id
                }.get(0)
            }

and a when statement and they all throw the same exception

here is my variable

lateinit var currentJob: JobPW

Any ideas? This is really weird. I'm thinking of raising a bug report but I'm not sure where to do it.

Upvotes: 0

Views: 304

Answers (2)

Cililing
Cililing

Reputation: 4763

It's because you tried to access (via == null) not initialized lateinit variable.

Let's check what docs tells us about it:

For example, properties can be initialized through dependency injection, or in the setup method of a unit test. In these cases, you cannot supply a non-null initializer in the constructor, but you still want to avoid null checks when referencing the property inside the body of a class.

So, such code always throws exception:

class Test {

    lateinit var lateinitAny: Any

    @Test
    fun test() {
        if (lateinitAny == null) { // throws kotlin.UninitializedPropertyAccessException
            println()
        }
    }
}

So your case looks like invalid usage of lateinit property. Those variables are usually handled by the DI frameworks etc. In your case, you just should use nullable property:

var currentJob: Job? = null

Then your code should work fine. However, you can still check if the property is initialized by this::lateinitAny.isInitialized. But, like I said, lateinit is designed for something else than "standard" variable usage.

ref: https://kotlinlang.org/docs/properties.html#late-initialized-properties-and-variables

Upvotes: 2

ninhnau19
ninhnau19

Reputation: 777

You need set value for lateinit var variable before use it.

To check variable is inited or not, you can check it with:

this::currentJob.isInitialized

Upvotes: 1

Related Questions