Parameter specified as non-null is null error in Room Database - android

Here is my entity class:
#Entity
data class User(
#PrimaryKey
#Json(name = "id") val userId: String,
#Json(name = "login") val userName: String,
#Json(name = "avatar_url") val userAvatar: String,
val profile: Profile? = null
) : Serializable
Here is my Profile data class
data class Profile(
val avatar_url: String,
val bio: String,
val blog: String,
val company: Any,
val created_at: String,
val email: Any,
val events_url: String,
val followers: Int,
val followers_url: String,
val following: Int,
val following_url: String,
val gists_url: String,
val gravatar_id: String,
val hireable: Boolean,
val html_url: String,
val id: Int,
val location: String,
val login: String,
val name: String,
val node_id: String,
val organizations_url: String,
val public_gists: Int,
val public_repos: Int,
val received_events_url: String,
val repos_url: String,
val site_admin: Boolean,
val starred_url: String,
val subscriptions_url: String,
val twitter_username: Any,
val type: String,
val updated_at: String,
val url: String
) : Serializable
but every time I try to insert data into the table I am getting the error, how can I insert null data object in table while using room database?

The issue here is that Room doesn't know how to insert attribute of type Profile to the table.
The simple solution would be to use a type converter. Something like the following:
class DatabaseConverters {
#TypeConverter
fun toProfile(profileJson: String): Profile? {
return <Create a Profile object out of a JSON string>
}
#TypeConverter
fun fromProfile(profile: Profile?): String {
return <JSON string representation of Profile object>
}
}
In your case - you can use "" (empty string) when Profile is null.
More info about converters: Here

Related

Can't parse json correctly

need help in parsing this json file from this endpoint
http://ergast.com/api/f1/constructors
this is my data class
data class Teams(
val MRData: MRdata
){
data class MRdata(
val ConstructorTable: ConstructorsTable,
val limit: String,
val offset: String,
val series: String,
val total: String,
val url: String,
val xmlns: String
){
data class ConstructorsTable(
val Constructors: List<Constructor>?
) {
data class Constructor(
val constructorId: String?,
val name: String?,
val nationality: String?,
val url: String?
)
}
}
}
when i use Teams class as a return model it logs actual data but, when i try to return specific data like constructorID or nationality it returns null.

Cant figure out return type of room db

My Entity
#Entity(tableName = "my_entity_table")
internal data class MYEntity(
#PrimaryKey(autoGenerate = true)
val pcID: Int,
val server_id: String,
val name: String,
val detail: String,
val duration: Int,
val start_date : Long,
val end_date: Long,
val created_by_user_id: String,
val db_create_dttm: Long,
val db_update_dttm: Long,
val db_create_src: Long,
val db_update_src: Long
)
My dao query where I to select the duration,start_date and end_date
#Query("select duration,start_date,end_date from my_entity_table where id =:pcID")
suspend fun getDurationDetails(pcID:Int) : What should be the return type?
What should be the return type of the query ??
You could make a specific data class as model for your query with the specific fields:
data class ExampleModel(
#ColumnInfo(name = "duration") val duration: Int?,
#ColumnInfo(name = "start_date") val start_date: Long?
#ColumnInfo(name = "end_date") val end_date: Long?
)
And then use it as return:
#Query("select duration,start_date,end_date from my_entity_table where id =:pcID")
suspend fun getDurationDetails(pcID:Int) : ExampleModel
I don't think that using the Entity model is posible cause your Entity parameters are not nullable and you query just a few of that parameters.

FOREIGN KEY constraint failed (code 787 SQLITE_CONSTRAINT_FOREIGNKEY)

I have no idea why i got this error when i invoke createBrowseRecord() function. My codes as follow, any ideas would be appreciated.
#Insert(onConflict = OnConflictStrategy.REPLACE)
fun createBrowseRecord(br:BrowseRecord)
#Entity(foreignKeys = [ForeignKey(entity = Composition::class,parentColumns =["id"],childColumns = ["compositionId"] ,onDelete = ForeignKey.CASCADE)])
data class BrowseRecord(#PrimaryKey val compositionId:String, val browseTime:Long)
#Entity
data class Composition(
val author: String,
val commentAmount: Int,
var cover: String,
val description: String,
val downloadURL: String,
val backGround:String,
val duration: String,
#PrimaryKey val id: String,
var isLike: Boolean,
val likeAmount: Int,
val title: String
)

Data Class and properties from other Data Class

I'm downloading few lists (RawAlbum, RawPhoto) from the web and I want to use properties from them to create another list. Then I want to use it to make a recyclerview.
So that's how it looks. It is the ListItem:
#Parcelize
data class ListItem(
val id: Int,
val title: String,
val albumTitle: String,
val thumbnailUrl: String
) : Parcelable
So I want to use the id and title from the RawAlbum. While the albumTitle and thumbnailUrl from the RawPhoto.
RawAlbum:
#Parcelize
#Entity(tableName = "albums")
data class RawAlbum(
#PrimaryKey
val id: Int,
val userId: Int,
val title: String
) : Parcelable
RawPhoto:
#Parcelize
#Entity(tableName = "photos")
data class RawPhoto(
#PrimaryKey
val id: Int,
val albumId: Int,
val title: String,
val url: String,
val thumbnailUrl: String
) : Parcelable

How to create constructor for data class in Kotlin Android?

How to create constructor for data class in Kotlin Android ?
data class EventItem(
#SerializedName("dateEvent")
val dateEvent: String,
#SerializedName("dateEventLocal")
val dateEventLocal: Any,
#SerializedName("idAwayTeam")
val idAwayTeam: String,
#SerializedName("idEvent")
val idEvent: String,
#SerializedName("idHomeTeam")
val idHomeTeam: String,
#SerializedName("idLeague")
val idLeague: String,
#SerializedName("idSoccerXML")
val idSoccerXML: String,
#SerializedName("intAwayScore")
val intAwayScore: Any,
#SerializedName("intAwayShots")
val intAwayShots: Any,
#SerializedName("intHomeScore")
val intHomeScore: Any,
#SerializedName("intHomeShots")
val intHomeShots: Any,
#SerializedName("intRound")
val intRound: String,
#SerializedName("intSpectators")
val intSpectators: Any,
#SerializedName("strAwayFormation")
val strAwayFormation: Any,
#SerializedName("strAwayGoalDetails")
val strAwayGoalDetails: String,
#SerializedName("strAwayLineupDefense")
val strAwayLineupDefense: String,
#SerializedName("strAwayLineupForward")
val strAwayLineupForward: String,
#SerializedName("strAwayLineupGoalkeeper")
val strAwayLineupGoalkeeper: String,
#SerializedName("strAwayLineupMidfield")
val strAwayLineupMidfield: String,
#SerializedName("strAwayLineupSubstitutes")
val strAwayLineupSubstitutes: String,
#SerializedName("strAwayRedCards")
val strAwayRedCards: String,
#SerializedName("strAwayTeam")
val strAwayTeam: String,
#SerializedName("strAwayYellowCards")
val strAwayYellowCards: String,
#SerializedName("strBanner")
val strBanner: Any,
#SerializedName("strCircuit")
val strCircuit: Any,
#SerializedName("strCity")
val strCity: Any,
#SerializedName("strCountry")
val strCountry: Any,
#SerializedName("strDate")
val strDate: String,
#SerializedName("strDescriptionEN")
val strDescriptionEN: Any,
#SerializedName("strEvent")
val strEvent: String,
#SerializedName("strEventAlternate")
val strEventAlternate: String,
#SerializedName("strFanart")
val strFanart: Any,
#SerializedName("strFilename")
val strFilename: String,
#SerializedName("strHomeFormation")
val strHomeFormation: Any,
#SerializedName("strHomeGoalDetails")
val strHomeGoalDetails: String,
#SerializedName("strHomeLineupDefense")
val strHomeLineupDefense: String,
#SerializedName("strHomeLineupForward")
val strHomeLineupForward: String,
#SerializedName("strHomeLineupGoalkeeper")
val strHomeLineupGoalkeeper: String,
#SerializedName("strHomeLineupMidfield")
val strHomeLineupMidfield: String,
#SerializedName("strHomeLineupSubstitutes")
val strHomeLineupSubstitutes: String,
#SerializedName("strHomeRedCards")
val strHomeRedCards: String,
#SerializedName("strHomeTeam")
val strHomeTeam: String,
#SerializedName("strHomeYellowCards")
val strHomeYellowCards: String,
#SerializedName("strLeague")
val strLeague: String,
#SerializedName("strLocked")
val strLocked: String,
#SerializedName("strMap")
val strMap: Any,
#SerializedName("strPoster")
val strPoster: Any,
#SerializedName("strResult")
val strResult: Any,
#SerializedName("strSeason")
val strSeason: String,
#SerializedName("strSport")
val strSport: String,
#SerializedName("strTVStation")
val strTVStation: Any,
#SerializedName("strThumb")
val strThumb: Any,
#SerializedName("strTime")
val strTime: String,
#SerializedName("strTimeLocal")
val strTimeLocal: String,
#SerializedName("strTweet1")
val strTweet1: Any,
#SerializedName("strTweet2")
val strTweet2: Any,
#SerializedName("strTweet3")
val strTweet3: Any,
#SerializedName("strVideo")
val strVideo: Any
) {
constructor(
idEvent: String,
strEvent: String,
strDate: String,
idHomeTeam: String,
strHomeTeam: String,
intHomeScore: Any,
idAwayTeam: String,
strAwayTeam: String,
intAwayScore: Any
) : this(idEvent, strEvent, strDate, idHomeTeam, strHomeTeam, intHomeScore, idAwayTeam, strAwayTeam, intAwayScore)
}
screenshot : https://i.stack.imgur.com/riGkU.png
How to create constructor for data class in Kotlin Android ?
i've try to create constructor. But, i get "There's a cycle in the delegation calls chain"
please, correct my code and tell me solution for it. . .
For a data class, the constructor defined in the class header is the primary constructor. It can't delegate to any other constructor in that class (though it can delegate to a superclass constructor).
However, you can define secondary constructors in the body of the class, as long as those delegate to the primary constructor. For example:
data class EventItem(val dateEvent: String) {
constructor(date: Date) : this(date.toString())
}
The problem in the code you've posted is that your secondary constructor is trying to delegate back to itself. You have to delegate to the primary constructor instead, and that means you need to be able to determine a value for all of the parameters missing from the secondary constructor.

Categories

Resources