Architecture-components-samples: Error debugging is a pain for this case

Created on 4 Jun 2017  路  10Comments  路  Source: android/architecture-components-samples

Im not sure if this is a "Bug", but after days of debugging, using several dagger architectures and so on i figured out why room throws the error

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).

I was not able to find the error using gradle. Even after using verbose assembleDebug there was no way to figure out why its just throwing a bunch of errors.

The reason for the error was, that i have in one Entitity

public class MyClass {
    @Embedded  private List<Condition> conditions;
}

@TypeConverter public static List<Condition> stringToConditionList(String data) {
        return GsonUtil.gson().fromJson(data, new TypeToken<List<Condition>>() {
        }.getType());
    }

@TypeConverter public static String fromConditionList(List<Condition> data) {
        return GsonUtil.gson().toJson(data);
    }

Since i have created a typeconverter which converts to string and back, its of course not possible to have embedded classes. Anyway, it was not possible to find any way to get the proper location for this.

Most helpful comment

@mynote If I could hug you, I would. This thread saved my ass!

All 10 comments

which Android studio version are you using?
I heard that in 3.0, clicking the error does not necessarily take you to the source of the problem.

We use a mechanism provided by javac to report errors so you should be able to click on the error and see the problem (or alternatively, from command line, you should see the file path with the line number).

AS 3.0 RC1+ (now 3)

@mynote If I could hug you, I would. This thread saved my ass!

no problem, just upvote if it helped to let others know that this is the "solution".

@yigit Any plans on fixing this issue? I would call it an issue since the error message doesn't describe exactly what's wrong. I ran into the same issue, because I had annotated a field with @Embedded.

Now I was also refactoring my code, so I didn't know which model was broken - and I have quite a few.

I'm using Android Studio 3.0.1 stable version and I couldn't double-click the error message as I would usually be able to, to make it take me to the file and as @mynote stated, it didn't help running the Gradle build with either verbose or stacktrace enabled.

EDIT: I guess it might be an issue related to Android Studio or IntelliJ itself, so maybe it should be reported to another team instead?

it took me hours to find out that the @Embedded was the issue, thanks @mynote

Thanks @mynote

i have exactly the same issue . But i want my list to be inside(embedded) the parent object. without using @embedded annotation, how else can i achieve this ?

A few days ago I wrote a question in Stackoverflow (https://stackoverflow.com/questions/61963144/how-to-make-a-typeconverter-in-room-from-a-custom-list/62020630#62020630) but I did not get the answer I needed, I was researching and found the solution, it worked for me I do not know if it will be worth it @namdiemefo

After several days carefully studying more about the advanced inserts of Room, I have discovered how to make the TypeConverter for a specific custom object, In my case ServersEntity

@TypeConverter
fun stringToListServer(data: String?): List<ServerEntity?>? {
    if (data == null) {
        return Collections.emptyList()
    }
    val listType: Type = object :
        TypeToken<List<ServerEntity?>?>() {}.type
    return gson.fromJson<List<ServerEntity?>>(data, listType)
}

@TypeConverter
fun listServerToString(someObjects: List<ServerEntity?>?): String? {
    return gson.toJson(someObjects)
}

On the other hand to convert the String lists, it would simply be done as follows

@TypeConverter
fun fromString(value: String?): List<String> {
    val listType = object :
        TypeToken<ArrayList<String?>?>() {}.type
    return Gson().fromJson(value, listType)
}

@TypeConverter
fun fromList(list: List<String?>?): String {
    val gson = Gson()
    return gson.toJson(list)
}

oh my god, thx very much @mynote

Was this page helpful?
0 / 5 - 0 ratings