
mcpierce
Kotlin and Android Development featuring Jetpack: Unique constraint error when running chapter 5 code changes
@mfazio23
I’ve applied the changes from Chapter 5 of the book and everything builds correctly and runs. But, when I try to start a game, the following exception is being thrown:
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: game_statuses.gameId, game_statuses.playerId (code 1555 SQLITE_CONSTRAINT_PRIMARYKEY)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:940)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:790)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:89)
at androidx.sqlite.db.framework.FrameworkSQLiteStatement.executeInsert(FrameworkSQLiteStatement.kt:42)
at androidx.room.EntityInsertionAdapter.insert(EntityInsertionAdapter.kt:85)
at dev.mfazio.pennydrop.data.PennyDropDao_Impl$10.call(PennyDropDao_Impl.java:278)
at dev.mfazio.pennydrop.data.PennyDropDao_Impl$10.call(PennyDropDao_Impl.java:273)
<snipped>
Any suggestion on what to check in the project code?
Marked As Solved

mfazio23
Apologies for losing track of this thread, but on the plus side, I see what’s going on. It took me a bit to find, too, since it wasn’t clear right away.
Quick version:
You’re missing android:text="@={player.playerName}"
on the @+id/edit_text_player_name
<Edit Text>
in player_list_item.xml
.
Long version:
The database exception happens because the game sees you as trying to enter in the same player twice, meaning both players have the same ID. They both are seen as having the same ID because of how the app gets players in PennyDropDao
:
// PennyDropDao.startGame(...)
val playerIds = players.map { player ->
getPlayer(player.playerName)?.playerId ?: insertPlayer(player)
}
We get players by name from the DB or create new versions. If two players have the same name (even if it’s a blank name), one is inserted into the DB and the other is retrieved right away, but they’re both the same.
In this issue’s case, the names were blank because nothing was in place to assign the name to the playerName
field. That lives inside player_list_item.xml
and the edit_text_player_name
<EditText>
.
If you don’t have a value set for android:text
, a TextView
will still show what you type in, but nothing happens with it. You need to set an expression there (with the =
after the @
) to link it back to the NewPlayer
object:
android:text="@={player.playerName}"
While the error makes sense in the end, it’s almost misleading given where the issue starts out.
Popular Prag Prog topics










Other popular topics










Latest in PragProg
Latest (all)
Categories:
Popular Portals
- /elixir
- /opensuse
- /rust
- /kotlin
- /ruby
- /erlang
- /python
- /clojure
- /react
- /quarkus
- /go
- /vapor
- /v
- /react-native
- /wasm
- /django
- /security
- /nodejs
- /centos
- /rails
- /haskell
- /fable
- /gleam
- /js
- /swift
- /deno
- /tailwind
- /assemblyscript
- /laravel
- /symfony
- /phoenix
- /crystal
- /typescript
- /debian
- /adonisjs
- /julia
- /arch-linux
- /svelte
- /spring
- /c-plus-plus
- /preact
- /flutter
- /actix
- /java
- /angular
- /ocaml
- /zig
- /kubuntu
- /scala
- /zotonic
- /vim
- /rocky
- /lisp
- /html
- /keyboards
- /vuejs
- /nim
- /emacs
- /elm
- /nerves