Blaise Lukwayi N
Blaise Lukwayi N

Reputation: 327

How do i properly query a room database

So I have a a database that I am trying to query, but I keep getting null objects when my activity calls the query method

Heres the Workout Class

@Entity(tableName = "workout_table")
public class Workout {

@PrimaryKey
@NonNull
private String name;


@ColumnInfo(name = "number_of_sets")
private int numOfSets;

@ColumnInfo(name = "workout_duration")
private int workDuration;

@ColumnInfo(name = "rest_duration")
private int restDuration;

@ColumnInfo(name = "selected_workout")
private boolean seletectedWorkout;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getNumOfSets() {
    return numOfSets;
}

public void setNumOfSets(int numOfSets) {
    this.numOfSets = numOfSets;
}

public int getWorkDuration() {
    return workDuration;
}

public void setWorkDuration(int workDuration) {
    this.workDuration = workDuration;
}

public int getRestDuration() {
    return restDuration;
}



public void setRestDuration(int restDuration) {
    this.restDuration = restDuration;
}

public boolean isSeletectedWorkout() {
    return seletectedWorkout;
}

public void setSeletectedWorkout(boolean seletectedWorkout) {
    this.seletectedWorkout = seletectedWorkout;
}

public Workout(String name, int numOfSets, int workDuration, int restDuration) {
    this.name = name;
    this.numOfSets = numOfSets;
    this.workDuration = workDuration;
    this.restDuration = restDuration;
}
public Workout(String name, int numOfSets, int workDuration, int restDuration, boolean 
selectedWorkout) {
    this.name = name;
    this.numOfSets = numOfSets;
    this.workDuration = workDuration;
    this.restDuration = restDuration;
    this.seletectedWorkout = selectedWorkout;
}
}

Here is me DAO

@Dao
public interface WorkoutDao {
@Insert(onConflict = OnConflictStrategy.IGNORE)
void insert(Workout workout);

@Delete
void delete(Workout workout);

@Query("select * from workout_table order by name ASC")
LiveData<List<Workout>> getAlphabetizedWorkouts();

@Query("select * from workout_table where selected_workout")
LiveData<Workout> getSelectedWorkout();
}

Here's the Repository

public class WorkoutRepository {
private WorkoutDao mWorkoutDao;

private LiveData<List<Workout>> mAllWorkouts;
private LiveData<Workout> mSelectedWorkout;
private static volatile WorkoutRepository sInstance = null;


WorkoutRepository(Application application) {
    WorkoutRoomDatabase db = WorkoutRoomDatabase.getDatabase(application);
    mWorkoutDao = db.workoutDao();
    mAllWorkouts = mWorkoutDao.getAlphabetizedWorkouts();
}

LiveData<List<Workout>> getAllWorkouts() {
    return mAllWorkouts;
}

LiveData<Workout> getSelectedWorkout() {
    return mSelectedWorkout;
}

void insert(Workout workout) {
    WorkoutRoomDatabase.databaseWriteExecutor.execute(() -> {
        mWorkoutDao.insert(workout);
    });

}
}

Here's the MainActivity class fields

 private MainActivityViewModel mMainActivityViewModel;
 private LiveData<Workout> defaultWorkout;

Here's MainActivity onCreate where the method call is

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mCountDown = findViewById(R.id.main_countdown);

    mButtonStartPause = findViewById(R.id.button_start_pause);
    mButtonReset = findViewById(R.id.button_reset);
    mNewWorkout = findViewById(R.id.new_workout);
    mWorkouts = findViewById(R.id.my_workouts);
    mWorkRestLabel = findViewById(R.id.work_rest_label);

    mMainActivityViewModel = new ViewModelProvider(this).get(MainActivityViewModel.class);
    defaultWorkout = mMainActivityViewModel.getSelectedWorkout();

    //set up the workout parameters
    mNumberOfSets = defaultWorkout.getValue().getNumOfSets();
    mWorkoutDuration = (int) (defaultWorkout.getValue().getWorkDuration())* 1000;
    mRestDuration = (int) (defaultWorkout.getValue().getRestDuration()) * 1000;
    }

each time I call the getSelectedworkout from MainActivity, I get A null pointer Exception is there something wrong with the method call or the query?

Upvotes: 1

Views: 50

Answers (1)

Shalu T D
Shalu T D

Reputation: 4039

In your Dao, last method getSelectedWorkout() should be changed as below:

@Query("select * from workout_table where selected_workout= :workout")
LiveData<Workout> getSelectedWorkout(int workout);

Upvotes: 1

Related Questions