0
votes

I'm using GSON to parse JSON from a web service. I don't have any issues with this on my test device or in the simulator, but I'm getting NullPointerException crashes in the Pre-Launch Report testing on the Play Console when I try to access one of the List objects.

I've tried checking the object for null before accessing it, but I'm still seeing the crash in the Pre-Launch Reports.

Here's the Hourly data class which contains the List object that's causing the crash:

data class Hourly(
    @SerializedName("data") val data: List<Conditions> = listOf(),
    @SerializedName("summary") val summary: String = "" //Partly cloudy in the evening
)

Here's where I'm accessing the object (with a bunch of extra checks to make sure the objects aren't null before accessing them):

if (forecastData == null || forecastData?.currently == null || forecastData?.hourly == null || forecastData?.hourly?.data == null)
    return

val hourlyData = forecastData?.hourly?.data ?: return
if (forecastData?.hourly != null && forecastData?.hourly?.data != null && hourlyData.count() > 2) {
    val nextHour = hourlyData[1]
    pressure = nextHour.pressure
}

One other thing to note is that this code was all working fine for the past year, but when I updated the app to API 28 and Kotlin 1.3.50 this crash started happening.

Update

I tried setting pretty much every object in my data model class to nullable, but I'm still getting the crash. Here's the end of the crash log, in case that is helpful:

 FATAL EXCEPTION: main
 Process: com.xxx.xxxx, PID: 27844
 java.lang.NullPointerException: throw with null exception
    at com.xxx.xxxx.model.Hourly.getData(Hourly.java:3)
    at com.xxx.xxxx.view.UserInterface.updateCurrently(UserInterface.java:38)

I assume based on this that I am supposed to check for null on hourly.data...

1
Any chance that there's a null element in your list? That is, rather than List<Condition>, perhaps it should be List<Condition?>Ben P.
You should have another Conditions data class where you can make the properties nullable those might be null response.0xAliHn
I tried setting every single item in my data classes to nullable, and I'm still seeing the crash. I will update the question with the crash log to see if that helps narrow things down at all.bmueller

1 Answers

0
votes

I have no idea why, but turning off minifyEnabled and turning on multidexEnabled in my release build settings fixed this bug. Glad I spent 3 whole days on this.