Family my app closes when it reaches the 2 line of code:
private MyDataBase dataBase = new MyDataBase(context);
private List<Nota> itemsImportantes =dataBase.getAllDataImportantes();
The LogCat tells me NullPointExeption on that line. The getAllDataImportantes () method it returns a list < > and I break my head trying to solve this error.
this is the method:
public List<Nota> getAllDataImportantes(){
List<Nota> Notas = new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("Select * From "+TABLE_NAME2,null);
while (res.moveToNext()){
Nota notaActual = new Nota();
notaActual.setId(res.getInt(0));
notaActual.setTitulo(res.getString(1));
notaActual.setDescripcion(res.getString(2));
Notas.add(notaActual);
}
db.close();
return Notas;
}
This is the error in the logcat:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.school.omg.mynote.MyDataBase.getAllDataImportant ()' on a null object reference
This is my Database class
public class MyDataBase extends SQLiteOpenHelper {
public static final String DATABASE_NAME="NotasDB.db";
public static final String TABLE_NAME="Notas";
public static final String TABLE_NAME2="Notas_Importantes";
public static final String TABLE_NAME3="Notas_Recordatorios";
public static final String COL_0="ID";
public static final String COL_1="TITULO";
public static final String COL_2="CUERPO";
public MyDataBase(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, TITULO TEXT, CUERPO TEXT)");
db.execSQL("CREATE TABLE " + TABLE_NAME2+ "(ID INTEGER PRIMARY KEY AUTOINCREMENT, TITULO TEXT, CUERPO TEXT)");
db.execSQL("CREATE TABLE " + TABLE_NAME3+ "(ID INTEGER PRIMARY KEY AUTOINCREMENT, TITULO TEXT, CUERPO TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
}
public void InsertData(String Titulo, String Cuerpo){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL_1,Titulo);
values.put(COL_2, Cuerpo);
db.insert(TABLE_NAME,null,values);
db.close();
}
public List<Nota> getAllData(){
List<Nota> Notas = new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("Select * From "+TABLE_NAME,null);
while (res.moveToNext()){
Nota notaActual = new Nota();
notaActual.setId(res.getInt(0));
notaActual.setTitulo(res.getString(1));
notaActual.setDescripcion(res.getString(2));
Notas.add(notaActual);
}
db.close();
return Notas;
}
public ArrayList<Nota> getAllDataImportantes(){
ArrayList<Nota> Notas = new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("Select * From "+TABLE_NAME2,null);
while (res.moveToNext()){
Nota notaActual = new Nota();
notaActual.setId(res.getInt(0));
notaActual.setTitulo(res.getString(1));
notaActual.setDescripcion(res.getString(2));
Notas.add(notaActual);
}
db.close();
return Notas;
}
public void InsertDataImportantes(String Titulo, String Cuerpo){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL_1,Titulo);
values.put(COL_2, Cuerpo);
db.insert(TABLE_NAME2,null,values);
db.close();
}
public List<Nota> getAllDataRecordatorio(){
List<Nota> Notas = new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("Select * From "+TABLE_NAME3,null);
while (res.moveToNext()){
Nota notaActual = new Nota();
notaActual.setId(res.getInt(0));
notaActual.setTitulo(res.getString(1));
notaActual.setDescripcion(res.getString(2));
Notas.add(notaActual);
}
db.close();
return Notas;
}
public void InsertDataRecordatorio(String Titulo, String Cuerpo){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL_1,Titulo);
values.put(COL_2, Cuerpo);
db.insert(TABLE_NAME3,null,values);
db.close();
}
}
Help Thank you