Reputation: 23
I am getting the following error after deleting the RoomDB data:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String app.zimablue.artbookfragmentversion.model.ArtEntity.getArtName()' on a null object reference
I think I am getting this error because I am trying to access the deleted data again, because I can delete the data, but I get an error because there is no data that will appear on the screen after it is deleted.
@Dao
interface ArtDao {
@Insert
suspend fun insert(artEntity: ArtEntity)
@Delete
suspend fun delete(artEntity: ArtEntity)
@Query("SELECT * FROM `art-table`")
fun getArtWithNameAndId():Flow<List<ArtEntity>>
@Query("SELECT * FROM `art-table` WHERE id = :id")
fun getArtById(id: Int): Flow<ArtEntity>
@Entity(tableName = "art-table")
class ArtEntity(
@ColumnInfo(name = "name")
@NonNull var artName : String,
@NonNull @ColumnInfo(name = "artistname")
var artistName: String,
@NonNull @ColumnInfo(name = "year")
var year : String,
@NonNull @ColumnInfo(name = "image")
var image : ByteArray
) {
@PrimaryKey(autoGenerate = true)
var id : Int = 0
}
private fun oldArtDetails(art : ArtEntity){
artFromMain = art
art.artName.let {
binding.artText.setText(it)
}
art.artistName.let {
binding.artistText.setText(it)
}
art.year.let {
binding.yearText.setText(it)
}
art.image.let {
val bitmap = it.let {
BitmapFactory.decodeByteArray(it,0, it.size) }
binding.imageView.setImageBitmap(bitmap)
}
}
I have uploaded the version of the application where the error is occurring to Github: https://github.com/mertparlak-zima/ArtBookFragment
Translation: I checked if "art.name" and "art.artistName" are null, but it didn't work. I made it nullable in the database, but it still didn't work.
Upvotes: 2
Views: 97
Reputation: 696
In your ArtDao return type is set to non-nullable, so dao returns ArtEntity that actually null but it can't be checked that it is not null.
Solution:
@Query("SELECT * FROM `art-table` WHERE id = :id")
fun getArtById(id: Int): Flow<ArtEntity?>
Upvotes: 1