I’m in doubt as to whether I can use the @NonNull parameter in my code. I created a migrations for him, but this is giving an error in the table, because there is data that I passed to the database and he asks "NotNul = truth". I researched about this error and it appeared as a solution the use of @NonNull and @NotNull, I want to know if it is possible to correct with these assignments
Log Fatal Exception:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pokedex, PID: 21162
java.lang.IllegalStateException: Migration didn't properly handle: PokemonItem(com.example.pokedex.model.PokemonItem).
Expected:
TableInfo{name='PokemonItem', columns={abilities=Column{name='abilities', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, hp=Column{name='hp', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, nome=Column{name='nome', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, types=Column{name='types', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=1, defaultValue='null'}}, foreignKeys=[], indices=[]}
Found:
TableInfo{name='PokemonItem', columns={abilities=Column{name='abilities', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, types=Column{name='types', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, hp=Column{name='hp', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, nome=Column{name='nome', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=1, defaultValue='null'}}, foreignKeys=[], indices=[]}
at androidx.room.RoomOpenHelper.onUpgrade(RoomOpenHelper.java:103)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onUpgrade(FrameworkSQLiteOpenHelper.java:124)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:416)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:316)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:92)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:53)
at androidx.room.RoomDatabase.inTransaction(RoomDatabase.java:476)
at androidx.room.RoomDatabase.assertNotSuspendingTransaction(RoomDatabase.java:281)
at androidx.room.RoomDatabase.query(RoomDatabase.java:324)
at androidx.room.util.DBUtil.query(DBUtil.java:83)
at com.example.pokedex.dao.PokemonDAO_Impl$3.call(PokemonDAO_Impl.java:101)
at com.example.pokedex.dao.PokemonDAO_Impl$3.call(PokemonDAO_Impl.java:98)
at androidx.room.CoroutinesRoom$Companion$createFlow$1$1.invokeSuspend(CoroutinesRoom.kt:81)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
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 Migrations:
package com.example.pokedex.service
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
class PokemonMigrations {
companion object{
val migration_1_2 = object : Migration(1,2){
// alteração do que deve ser NOT NULL para NULL
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL(
"CREATE TABLE 'Pokemon_Novo' "+
"('id' TEXT PRIMARY KEY NOT NULL, "+
"'nome' TEXT NOT NULL, "+
"'types' TEXT, "+
"'abilities' TEXT, "+
"'hp' TEXT )"
)
database.execSQL(
"INSERT INTO Pokemon_Novo (" +
"id, nome, types, abilities, hp)" +
"SELECT id, nome, types, abilities, hp FROM PokemonItem"
)
database.execSQL("DROP TABLE PokemonItem")
database.execSQL("ALTER TABLE Pokemon_Novo RENAME TO PokemonItem")
}
}
}
}
Source: Android Questions