Ошибка java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Iterator java.util.ArrayList.iterator().. при парсинге JSON с GSON

Нужно спарсить API с погодой. Но происходит ошибка. Смотрел в Интернете, на форумах и у всех код схож с моим, но у них все работает. Помогите пожалуйста! Ссылка на библиотеку Rx (на всякий случай): https://github.com/ReactiveX/RxAndroid

Log:

2024-08-22 18:24:46.676 26171-26215 GsonParsing             ru.arturprgr.mybrowser               D  https://api.open-meteo.com/v1/forecast?latitude=57.1768658&longitude=65.5983044&current=temperature_2m,relative_humidity_2m,apparent_temperature,precipitation,rain,surface_pressure&daily=temperature_2m_max,temperature_2m_min
2024-08-22 18:24:47.226 26171-26215 GsonParsing             ru.arturprgr.mybrowser               D  Все ок!
2024-08-22 18:24:47.229 26171-26171 GsonParsing             ru.arturprgr.mybrowser               E  java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Iterator java.util.ArrayList.iterator()' on a null object reference
                                                                                                        at ru.arturprgr.mybrowser.ui.fragment.main.MainFragment$onCreateView$permissions$1$1$3.invoke(MainFragment.kt:87)
                                                                                                        at ru.arturprgr.mybrowser.ui.fragment.main.MainFragment$onCreateView$permissions$1$1$3.invoke(MainFragment.kt:86)
                                                                                                        at ru.arturprgr.mybrowser.ui.fragment.main.MainFragment$onCreateView$permissions$1$1.invoke$lambda$2(MainFragment.kt:86)
                                                                                                        at ru.arturprgr.mybrowser.ui.fragment.main.MainFragment$onCreateView$permissions$1$1.$r8$lambda$3AB3huHcwTbT2YsCaQxu16rQsIA(Unknown Source:0)
                                                                                                        at ru.arturprgr.mybrowser.ui.fragment.main.MainFragment$onCreateView$permissions$1$1$$ExternalSyntheticLambda2.accept(D8$$SyntheticClass:0)
                                                                                                        at io.reactivex.internal.observers.LambdaObserver.onNext(LambdaObserver.java:63)
                                                                                                        at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(ObservableObserveOn.java:200)
                                                                                                        at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(ObservableObserveOn.java:252)
                                                                                                        at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:109)
                                                                                                        at android.os.Handler.handleCallback(Handler.java:958)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                                        at android.os.Looper.loopOnce(Looper.java:205)
                                                                                                        at android.os.Looper.loop(Looper.java:294)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:8282)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:554)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)

Подключаемые библиотеки:

implementation ("io.reactivex.rxjava3:rxandroid:3.0.2")
    implementation ("io.reactivex.rxjava3:rxjava:3.1.9")
    implementation("com.google.code.gson:gson:2.11.0")

Часть кода MainActivity в onCreate, где основная логика вопроса: Observable.create

val urlConnection = URL("https://api.open-meteo.com/v1/forecast?latitude=${location.latitude}&longitude=${location.longitude}&current=temperature_2m,relative_humidity_2m,apparent_temperature,precipitation,rain,surface_pressure&daily=temperature_2m_max,temperature_2m_min").openConnection() as HttpURLConnection
                                Log.d("GsonParsing", urlConnection.url.toString())
                                try {
                                    urlConnection.connect()
                                    if (urlConnection.responseCode != HttpURLConnection.HTTP_OK) {
                                        it.onError(
                                            RuntimeException(urlConnection.responseMessage)
                                        )
                                        Log.d("GsonParsing", "Все плохо!")
                                    } else {
                                        it.onNext(
                                            urlConnection.inputStream.bufferedReader().readText()
                                        )
                                        Log.d("GsonParsing", "Все ок!")
                                    }
                                } finally {
                                    urlConnection.disconnect()
                                }
                            }.map { Gson().fromJson(it, Weather::class.java) }
                                .subscribeOn(Schedulers.newThread())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe({
                                    for (item in it.items) Log.d(
                                        "GsonParsing",
                                        "Item: ${item.temperature2mMax}"
                                    )
                                }, {
                                    Log.e("GsonParsing", "", it)
                                })

Моделька WeatherItem:

package ru.arturprgr.mybrowser.model

data class WeatherItem(
    val temperature2m: Double,
    val temperature2mMax: Double,
    val temperature2mMin: Double,
    val apparentTemperature: Double,
    val relativeHumidity2m: Int,
    val precipitation: Double,
    val rain: Double,
    val surfacePressure: Double,
)

// {
// "latitude":52.52,
// "longitude":13.419998,
// "generationtime_ms":0.09202957153320312,
// "utc_offset_seconds":0, "timezone":"GMT",
// "timezone_abbreviation":"GMT", "elevation":38.0,
// "current_units":{ "time":"iso8601", "interval":"seconds", "temperature_2m":"°C", "relative_humidity_2m":"%", "apparent_temperature":"°C", "precipitation":"mm", "rain":"mm", "surface_pressure":"hPa" },
// "current":{ "time":"2024-08-22T07:30", "interval":900, "temperature_2m":16.3, "relative_humidity_2m":63, "apparent_temperature":14.9, "precipitation":0.00, "rain":0.00, "surface_pressure":1010.6 },
// "daily_units":{ "time":"iso8601", "temperature_2m_max":"°C", "temperature_2m_min":"°C" },
// "daily":{ "time":["2024-08-22", "2024-08-23", "2024-08-24", "2024-08-25", "2024-08-26", "2024-08-27", "2024-08-28"],
// "temperature_2m_max":[23.5, 29.7, 31.5, 23.8, 24.5, 26.3, 29.5],
// "temperature_2m_min":[14.1, 16.2, 19.2, 17.2, 14.8, 14.2, 17.8]
// }

Моделька Weather:

package ru.arturprgr.mybrowser.model

data class Weather (
    var items: ArrayList<WeatherItem>
)

Ответы (0 шт):