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?

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

Razor54672
The answer to 3rd Problem of Chapter 5 (Making Choices) of “Practical Programming, Third Edition” seems incorrect in the given answer ke...
New
yulkin
your book suggests to use Image.toByteData() to convert image to bytes, however I get the following error: "the getter ‘toByteData’ isn’t...
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...
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...
New
alanq
This isn’t directly about the book contents so maybe not the right forum…but in some of the code apps (e.g. turbo/06) it sends a TURBO_ST...
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 ...
New
AndyDavis3416
@noelrappin Running the webpack dev server, I receive the following warning: ERROR in tsconfig.json TS18003: No inputs were found in c...
New
adamwoolhether
Is there any place where we can discuss the solutions to some of the exercises? I can figure most of them out, but am having trouble with...
New
AufHe
I’m a newbie to Rails 7 and have hit an issue with the bin/Dev script mentioned on pages 112-113. Iteration A1 - Seeing the list of prod...
New
a.zampa
@mfazio23 I’m following the indications of the book and arriver ad chapter 10, but the app cannot be compiled due to an error in the Bas...
New

Other popular topics Top

wolf4earth
@AstonJ prompted me to open this topic after I mentioned in the lockdown thread how I started to do a lot more for my fitness. https://f...
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...
New
AstonJ
SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New
AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
New
AstonJ
In case anyone else is wondering why Ruby 3 doesn’t show when you do asdf list-all ruby :man_facepalming: do this first: asdf plugin-upd...
New
AstonJ
If you are experiencing Rails console using 100% CPU on your dev machine, then updating your development and test gems might fix the issu...
New
OvermindDL1
Woooooooo! This is such a huge release for it, and 2 years incoming! In short, the library is now using an updated hyper backend (not j...
New
Maartz
Hi folks, I don’t know if I saw this here but, here’s a new programming language, called Roc Reminds me a bit of Elm and thus Haskell. ...
New
PragmaticBookshelf
Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
New
New

Latest in PragProg

View all threads ❯