Henrique Mendes Henrique Mendes - 2 years ago 86
Android Question

Using room persistence when trying to fetch items or insert returns null database

Working with room persistence, when trying to get the database to insert or make select in the items, the error appears:


AppDataBase.getMovieDao()' on a null object reference


The classes related to the process are as follows:

AppDataBase class:

@Database(entities = {Movies.class}, version = 1)
public abstract class AppDataBase extends RoomDatabase {

public static final String DB_NAME = "Movies";
private static AppDataBase INSTANCE;

public static AppDataBase getDataBase(Context context){
if (INSTANCE == null){
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),AppDataBase.class,DB_NAME).build();
}
return INSTANCE;
}

public abstract MovieDao getMovieDao();
}


Dao Class:

@Dao
public interface MovieDao {

@Insert
void insertAll(Movies movies);

@Update
void updateAll(Movies... notes);

@Query("SELECT * FROM moviestb")
List<Movies> getAll();

@Delete
void deleteAll(Movies... notes);

}


Entity class:

@Entity(tableName = "moviestb")
public class Movies {

@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "idmovie")
private long id;

@ColumnInfo(name = "titlemovie")
private String titlemovie;
........
}


Searching for the registrations:

public void loadFromDB(){
db.getDataBase(view.getContext());
if(db.getMovieDao().getAll().size() > 0){
adapter.setResults(db.getMovieDao().getAll());
}else{
Toast.makeText(view.getContext(),"Não há filmes cadastrados",Toast.LENGTH_SHORT).show();
view.getActivity().finish();
}
}


Insert:

public View.OnClickListener onSaveClick(final String plot, final String diretor, final String autor,
final String nome, final String tipo, final String ano, final String ator, final String imdb) {
return new View.OnClickListener() {
@Override
public void onClick(View itemView) {
db.getDataBase(view.getContext());
Movies movies = new Movies(nome,plot,imdb,"",ator,ano,tipo,diretor,autor);
new InsertAsyncTask(db).execute(movies);

}
};
}

private class InsertAsyncTask extends AsyncTask<Movies,Void,Void>{
private AppDataBase db;
public InsertAsyncTask(AppDataBase appDataBase) {
db = appDataBase;
}

@Override
protected Void doInBackground(Movies... params) {
db.getMovieDao().insertAll(params[0]);
return null;
}
}


Searching all:

public void loadFromDB(){
db.getDataBase(view.getContext());
if(db.getMovieDao().getAll().size() > 0){
adapter.setResults(db.getMovieDao().getAll());
}else{
Toast.makeText(view.getContext(),"Não há filmes cadastrados",Toast.LENGTH_SHORT).show();
view.getActivity().finish();
}
}


The crash occurs while fetching the database, what am I doing wrong? thank you!

Answer Source

db is null, apparently. Your question does not show where you ever assign it a value.

Please note that getDataBase() is a static method. Perhaps you should have a db=AppDataBase.getDataBase() statement somewhere.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download