mcpierce

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?

0 1139 10

Marked As Solved

mfazio23

mfazio23

Author of Kotlin and Android Development featuring Jetpack

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 Top

belgoros
Following the steps described in Chapter 6 of the book, I’m stuck with running the migration as described on page 84: bundle exec sequel...
4 1149 1
New
jamis
The following is cross-posted from the original Ray Tracer Challenge forum, from a post by garfieldnate. I’m cross-posting it so that the...
4 2348 3
New
Alexandr
Hi everyone! There is an error on the page 71 in the book “Programming machine learning from coding to depp learning” P. Perrotta. You c...
5 1334 4
New
HarryDeveloper
Hi @venkats, It has been mentioned in the description of ‘Supervisory Job’ title that 2 things as mentioned below result in the same eff...
0 2203 2
New
Chrichton
Dear Sophie. I tried to do the “Authorization” exercise and have two questions: When trying to plug in an email-service, I found the ...
1 1178 3
New
leba0495
Hello! Thanks for the great book. I was attempting the Trie (chap 17) exercises and for number 4 the solution provided for the autocorre...
0 1147 3
New
patoncrispy
I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
5 1475 3
New
jskubick
I found an issue in Chapter 7 regarding android:backgroundTint vs app:backgroundTint. How to replicate: load chapter-7 from zipfile i...
0 3459 3
New
dsmith42
Hey there, I’m enjoying this book and have learned a few things alredayd. However, in Chapter 4 I believe we are meant to see the “&gt;...
0 1896 3
New
jonmac
The allprojects block listed on page 245 produces the following error when syncing gradle: “org.gradle.api.GradleScriptException: A prob...
1 2160 1
New

Other popular topics Top

AstonJ
If it’s a mechanical keyboard, which switches do you have? Would you recommend it? Why? What will your next keyboard be? Pics always w...
144 8219 50
New
Exadra37
Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it. Does your monitor have eye p...
227 8356 88
New
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
118 6884 31
New
Exadra37
On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
24 3382 25
New
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
82 6591 31
New
PragmaticBookshelf
“Finding the Boundaries” Hero’s Journey with Noel Rappin @noelrappin Even when you’re ultimately right about what the future ho...
34 3628 22
New
PragmaticBookshelf
Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
32 2728 10
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
8 3154 4
New
PragmaticBookshelf
Author Spotlight Rebecca Skinner @RebeccaSkinner Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
106 10209 29
New
PragmaticBookshelf
Author Spotlight Erin Dees @undees Welcome to our new author spotlight! We had the pleasure of chatting with Erin Dees, co-author of ...
24 3409 12
New

Latest in PragProg

View all threads ❯