I'm using Room as my local database for my project. I have created a MovieDao
interface:
@Dao
public interface MovieDao {
@Query("SELECT * FROM movies ORDER BY title ASC")
LiveData<List<Movies.Movie>> getMovies();
@Query("SELECT * FROM movies WHERE id = :id")
LiveData<Movies.Movie> getMovie(int id);
}
Everytime I try to call getMovies()
method, it correctly return a list of Movie
objects but if I call getMovie(int id)
and pass an id that exist for sure in the database I always get NPE.
Hoe can I create a query than can return a single Movie
object?
Thanks!
Edit:
LiveData<Movies.Movie> liveData = movieRepository.retrieveFavoriteMovieTask(550);
liveData.observe(this, m -> {
Log.d(TAG, m.title);
});
Error:
java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.example.myapp.Movies$Movie.title' on a null object reference
id may be reserved by room. You can set a auto incrementing PK with @PrimaryKey(autoGenerate = true) then define your own id (movie_id) that you can set and get at will while leaving the PK integrity intact.