epoxy icon indicating copy to clipboard operation
epoxy copied to clipboard

NullPointerException when Epoxy model is bind to data class

Open eric-ampire opened this issue 4 years ago • 7 comments

I have noticed that epoxy throws this exception when the model use the data class with kotlin 1.5.10 and 1.5.0

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
        at org.zxconnect.android.daylivro.models.entity.Product.hashCode(Unknown Source:19)
        at org.zxconnect.android.delivro.ProductBindingModel_.hashCode(ProductBindingModel_.java:330)

eric-ampire avatar May 26 '21 10:05 eric-ampire

sounds like a bug in your Product class's hashCode implementation calling hashCode on a null string

elihart avatar May 27 '21 22:05 elihart

Yes, it is the exception that is triggered, but if I remove when I remove the keyword data, there is no more exception

In my model there is no property with null as default value, The application does not trigger the exception if I use Kotlin version 1.4.30

eric-ampire avatar May 31 '21 10:05 eric-ampire

I had a non-nullable String variable in my data class but when my Api service started to send null variable to it, this error occurred.

Downgrade Kotlin version to 1.4.32 is also works but it's not a good option.

So i fixed this issue by converting some of my non-nullable variables to nullable variables like below.

Before:

data class ExampleClass {
      val myNullableString: String
      ..
}

After:

data class ExampleClass {
      val myNullableString: String?=null
      ..
}

tekinumut avatar Jul 16 '21 18:07 tekinumut

Hello, got the same error. I had data class inside first data class, so when this is null, it throws NPE for hashCode(). Appeared after updating Kotlin version to 1.5.0

image

zhambylgaziz avatar Feb 04 '22 05:02 zhambylgaziz

@zhambylgaziz i guess you must initialize your data class variable as nullable like me.

tekinumut avatar Feb 04 '22 06:02 tekinumut

@tekinumut Thanks, i stuck with same issue, your reply solved that :)

wh173d3v11 avatar Mar 04 '22 17:03 wh173d3v11

I don't get why the compiler can't just tell you which field is causing the issue.

martymiller avatar Mar 31 '22 19:03 martymiller