Error:Not sure how to convert a Cursor to this method's return type
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
Compilation failed; see the compiler error output for details.
Using Room
I'm getting this error and I'd like to find out which method causes it.
I have multiple DAO
s, with approximately 60 methods in total, and this error just popped up after adding a method (copy&pasted from another one that worked perfectly, just changed the field to set).
I could post the whole class of DAO
s, but I'm asking for a way to know which method failed. I tried with Run with --stacktrace
, Run with --info
and --debug option
, but none of these show any valuable information.
The method I added is a @Query
UPDATE
with Int
return type, as suggested in the documentation
UPDATE or DELETE queries can return void or int. If it is an int, the value is the number of rows affected by this query.
EDIT: I'd like to add that I tried deleting the method, bringing the DAO back to the working state, but it still gives me this error.
EDIT2: Adding gradle console output because unreadable in comments:
error: Not sure how to convert a Cursor to this method's return type
error: Not sure how to convert a Cursor to this method's return type
2 errors
:app:compileDebugJavaWithJavac FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
* Get more help at https://help.gradle.org
BUILD FAILED in 22s
Recently I've had the same problem but I was using Coroutines
within the Dao
function, e.g.:
@Query("SELECT * FROM Dummy")
suspend fun get(): LiveData<List<Dummy>>
And was unable to compile, but after removing the suspend
everything worked just fine. It's not needed when returning LiveData
. suspend
and LiveData
seem not to work together (as of now).
I Spend the entire day on this issue. the solution was very simple. I was using something like this before
@Query("SELECT * FROM myTable")
fun getAll(): MutableLiveData<ArrayList<myData>>
Now when I changed ArrayList to List & MutableLiveData to LiveData it is working fine.
@Query("SELECT * FROM myTable")
fun getAll(): LiveData<List<myData>>
based on the answers & comments on this issue I think room support only List & LiveData because I tried with on MutableLiveData & only ArrayList too. none of the combinations worked.
Hope this will help someones few hours.
For anyone landing here, using a coroutine Flow as a return type, you will get this error if you accidentally make the function suspend. Since it is returning a flow, there is no need to suspend.
So instead of this:
@Query("SELECT * FROM myTable WHERE id = :id")
suspend fun findById(id: Long): Flow<MyDataType>
use this (without suspend modifier):
@Query("SELECT * FROM myTable WHERE id = :id")
fun findById(id: Long): Flow<MyDataType>
Yup, based on what you have mentioned in the comments, you are not allowed to change the return type from List
to anything else inside the Dao
. I'd assume Room doesn't know how to deal with other return types. Take the List
and cast/convert it into your desired type outside of the Dao
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With