0

I am using exposed in one project, and I have a table lets call it TableX with two properties property1 and x knowing that x is nullable I added TableX.x.isNotNull() to my query so I can ignore null rows!.

And I have Object1 with also two properties as TableX which are: property1 and x knowing that x is not null in Object1

Then when I create Object1 out of the rows from the query, the compiler will nag about x because it should not be null and we are receiving a nullable x from TableX.

So I have added !! when setting x in Object1, given that I am sure that the query will never return any row with x is null because of the constraint that I have added.

But still, I receive KotlinNullPointerException some times. So how is this possible?

I thought of some compatibility issues between MySQL and exposed! But couldn't find any

   val result = listOf<Object1>()

   transaction {
        val query = TableX.select {
            TableX.property1.eq(123) and
            TableX.x.isNotNull()
        }
            .fetchSize(1000)

        result = query.map {
            Object1(
                property1 = it[TableX.property1],
                x = it[TableX.x]!!
            )
        }
    }
HSLM
  • 1,692
  • 10
  • 25
  • What database do you use? Could you ensure (by adding SQLLogger) that executed query have only `property1 = 123 and x is not null` in a where part? – Tapac Dec 13 '19 at 15:59

1 Answers1

0

I'm facing the same problem in a company I am working now. And it looks like you have race condition in place. Try to explicitly check for null before building result list. At least you won't get an exception.

Darmen Amanbay
  • 4,869
  • 3
  • 29
  • 50
  • Thank you for your response :). But the thing is adding 'where x is not null' supposed to be the `if` that I am relying on. And race condition should note occur here because of the transaction, what do you think? – HSLM Dec 13 '19 at 08:29