I have a problem, when inserting API response, to ...
# room
m
I have a problem, when inserting API response, to database, this is the error:
E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-3
Process: com.example.weatherappfp, PID: 26514
java.lang.NullPointerException: Attempt to invoke virtual method 'int com.example.forecastAppFP.data.db.entity.ForecastEntry.getId()' on a null object reference
at com.example.forecastAppFP.data.db.ForecastDao_Impl$1.bind(_*ForecastDao_Impl.java:34*_)
at com.example.forecastAppFP.data.db.ForecastDao_Impl$1.bind(_*ForecastDao_Impl.java:2*_6)
at androidx.room.EntityInsertionAdapter.insert(EntityInsertionAdapter.java:63)
at com.example.forecastAppFP.data.db.ForecastDao_Impl.upsert(_*ForecastDao_Impl.java:44*_)
at com.example.forecastAppFP.data.repository.ForecastRepositoryImpl$persistFetchedForecast$1.invokeSuspend(_*ForecastRepositoryImpl.kt:35*_)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
_*ForecastDao_Impl.java34 -->*_
Copy code
@Override
public void bind(SupportSQLiteStatement stmt, ForecastEntry value) {
  stmt.bindLong(1, value.getId());
}
_*ForecastDao_Impl.java:2*_6: -->
Copy code
this.__insertionAdapterOfForecastEntry = new EntityInsertionAdapter<ForecastEntry>(__db) {
  @Override
  public String createQuery() {
    return "INSERT OR ABORT INTO `forecast` (`id`) VALUES (?)";
  }

  @Override
  public void bind(SupportSQLiteStatement stmt, ForecastEntry value) {
    stmt.bindLong(1, value.getId());
  }
};
_*ForecastDao_Impl.java44 -->*_
Copy code
__insertionAdapterOfForecastEntry.insert(forecastEntry);
ForecastRepositoryImpl.kt35 -->
Copy code
private fun persistFetchedForecast(fetchedForecast: ForecastResponse) {
    GlobalScope.launch(<http://Dispatchers.IO|Dispatchers.IO>) {
        forecastDao.upsert(fetchedForecast.forecastEntry)
    }
}
use of
persistFetchedForecast
function:
Copy code
init {
    forecastNetworkDataSource.apply {
        downloadedForecast.observeForever { newWeather ->
            persistFetchedForecast(newWeather)
        }
    }
}
ForecastNetworkDataSource
interface:
Copy code
interface ForecastNetworkDataSource {
    val downloadedForecast: LiveData<ForecastResponse>

    suspend fun fetchForecast(
        //TODO Need to add lat and lon.?
    )
}
ForecastNetworkDataSourceImpl
class:
Copy code
class ForecastNetworkDataSourceImpl(
    private val forecastApiService: ForecastApiService
) : ForecastNetworkDataSource {

    private val _downloadedForecast = MutableLiveData<ForecastResponse>()
    override val downloadedForecast: LiveData<ForecastResponse>
        get() = _downloadedForecast

    //TODO Add lat and lon to this fun.
    override suspend fun fetchForecastDS() {
        try {
            val fetchedForecast = forecastApiService
                .getForecastAsync()
                .await()
            _downloadedForecast.postValue(fetchedForecast)
        }
        catch (e: NoConnectivityException) {
            Log.e("Connectivity", "No internet connection.", e)
        }
    }
}
ForecastResponse
class:
Copy code
data class ForecastResponse(
    @SerializedName("forecast")
    val forecastEntry: ForecastEntry
)
ForecastEntry
class: