Collection contains no element matching the predicate
Asked Answered
P

3

12

I'm using the Room library, and I'm getting the following error message when I try to build the app:

e: [kapt] An exception occurred: java.util.NoSuchElementException: Collection contains no element matching the predicate.

Here is a more detailed error message:

FAILURE: Build failed with an exception
* What went wrong:
Execution failed for task ':app:kaptDebugKotlin'.
> Compilation error. See log for more details

I isolated the problem in the code to be somewhere in the following, although I don't know what exactly is throwing this exception:

package com.example.pomoplay

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

@Database(entities = [(Category::class)], version = 1)
abstract class PomoPlayDatabase: RoomDatabase() {

    abstract fun categoryDao(): CategoryDao

    companion object {
        private var INSTANCE: PomoPlayDatabase? = null
        internal fun getDatabase(context: Context): PomoPlayDatabase?
        {
            if (INSTANCE == null) {
                synchronized(PomoPlayDatabase::class.java) {
                    if (INSTANCE == null) {
                        INSTANCE =
                            Room.databaseBuilder<PomoPlayDatabase>(
                                context.applicationContext,
                                PomoPlayDatabase::class.java,
                                "pomoplay_database").build()
                    }
                }
            }
            return INSTANCE
        }
    }
}

Gradle.build - project

    buildscript {
        ext.kotlin_version = '1.3.61'
        repositories {
            google()
            jcenter()

        }
        dependencies {


            classpath 'androidx.navigation:navigation-safe-args-gradle-plugin:2.1.0'
            classpath 'com.android.tools.build:gradle:3.5.3'
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
    }

    allprojects {
        repositories {
            google()
            jcenter()

        }
    }

    task clean(type: Delete) {
        delete rootProject.buildDir
    }

Gradle.build - module

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: "androidx.navigation.safeargs.kotlin"

apply plugin: 'kotlin-kapt'

android {

    packagingOptions {
        exclude 'META-INF/atomicfu.kotlin_module'
    }

    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.pomoplay"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.1.0'
    implementation 'androidx.navigation:navigation-ui-ktx:2.1.0'

    //Room Components
    implementation "androidx.room:room-runtime:2.2.3"
    kapt "androidx.room:room-compiler:2.2.3"

    //Testing Components
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

}
Peak answered 13/1, 2020 at 4:39 Comment(4)
can you share the code file where you are using the collection with predicate?Cohby
@DipankarBaghel I don't know where in the code this refers to.Peak
@DipankarBaghel I don't think I'm even using a predicate in my code.Peak
Compile time errors without line number are very irritating. That is where we are thrown in limboBryna
J
6

In my case its happening when or because im trying to make filter by one field that does not exist example

class Items(id:Int, name:String, isNew:Boolean)

so when I filter a list of "Items" with something like this

var list:Arraylist<Items>
list.filter{it->it.isNew}

if my backend does not have o retrieve that field "isNew" my code trow my that exceptions, that's was in my case

Johppah answered 20/6, 2020 at 5:15 Comment(0)
B
3

No need to create separate Data class. Just make sure its Serializable

After spending a day for this error, I identified the issue to be presence of an empty constructor in the @Entity class.

constructor() {}

Its not that always this constructor should be absent. I have another @Entity class where it is very much needed or else it fails with this error

Entities and POJOs must have a usable public constructor. 
You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
public final class Question implements java.io.Serializable

Not understood the reasoning behind it, but must be way of our usage. That shouldn't mean Android Studio should go silent on Line number of compilation error. It felt like a mouse trap

Bryna answered 2/3, 2020 at 18:51 Comment(1)
Pointing to proper hint.Hoedown
P
2

To track down the source of the problem, I took out all the files in the project and then put back one file at a time and isolated the problem to the room database class file I mentioned above in my original question. I thought that maybe the Category::class attribute in the array of the @Database annotation has something wrong with it.

I looked at how I constructed the Category class and compared it to how entity classes are constructed in other Android Room projects. Those projects used a Data class for the entities, whereas I used a regular POCO style structure. So, I implemented a Data class instead and now everything is working.

Category data class:

import androidx.annotation.NonNull
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "categories_table")
data class Category(@PrimaryKey(autoGenerate = true) @NonNull @ColumnInfo(name = "categoryId") val id: Int, @ColumnInfo(name = "categoryName") val name: String, @ColumnInfo(name = "categoryDesc") val desc: String)
Peak answered 13/1, 2020 at 7:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.