Search code examples
androidkotlinfatal-error

Fatal Exception Main error Null Pointer Exception


I am in error when running my application that the main one is not my converter. I don't know what to do to get it fixed, can someone help me?

Log Error:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pokedex, PID: 22909
java.lang.NullPointerException: Parameter specified as non-null is null: method 
 kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter value
    at com.example.pokedex.Converters.fromAbilityPokemonList(Unknown Source:2)
    at com.example.pokedex.dao.PokemonDAO_Impl$1.bind(PokemonDAO_Impl.java:62)
    at com.example.pokedex.dao.PokemonDAO_Impl$1.bind(PokemonDAO_Impl.java:36)
    at androidx.room.EntityInsertionAdapter.insert(EntityInsertionAdapter.java:63)
    at com.example.pokedex.dao.PokemonDAO_Impl$2.call(PokemonDAO_Impl.java:84)
    at com.example.pokedex.dao.PokemonDAO_Impl$2.call(PokemonDAO_Impl.java:79)
    at androidx.room.CoroutinesRoom$Companion$execute$2.invokeSuspend(CoroutinesRoom.kt:54)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
    at androidx.room.TransactionExecutor$1.run(TransactionExecutor.java:45)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:923)

Class Converters

class Converters {
// Conversor PokemonAbility
@TypeConverter
fun fromAbilityPokemonList(value: List<PokemonAbility>): String {
    val gson = Gson()
    val type = object : TypeToken<List<PokemonAbility>>() {}.type
    return gson.toJson(value, type)
}

@TypeConverter
fun toAbilityPokemonList(value: String): List<PokemonAbility> {
    val gson = Gson()
    val type = object : TypeToken<List<PokemonAbility>>() {}.type
    return gson.fromJson(value, type)
}

// Conversor Type Pokemon
@TypeConverter
fun fromTypePokemonList(value: List<String>): String {
    val gson = Gson()
    val type = object : TypeToken<List<String>>() {}.type
    return gson.toJson(value, type)
}

    @TypeConverter
    fun toTypePokemonList(value: String): List<String> {
    val gson = Gson()
    val type = object : TypeToken<List<String>>() {}.type
    return gson.fromJson(value, type)
}

This error started to occur after I made a Migrations to change from "Not Null to TEXT my db data


Solution

  • You are receiving value: List null in fromAbilityPokemonList. Instead use List?

    @TypeConverter
    fun fromAbilityPokemonList(value: List<PokemonAbility>?): String {
        val gson = Gson()
        val type = object : TypeToken<List<PokemonAbility>>() {}.type
        return gson.toJson(value ?: ArrayList<PokemonAbility>(), type)
    }