Error in a List

2

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

    
asked by Erio Cedeno 23.11.2018 в 04:07
source

1 answer

2

If it's how I see your code, the problem is that you're defining your properties in the class space, but one depending on another and you do not have time to assign the value.

You must create a method in your class that initializes the variable database , followed by the variable itemsImportantes . For example:

// La declaras, ambas almacenan un valor nulo
private MyDataBase dataBase;
private List<Nota> itemsImportantes; 

 . . .

// En tiempo de ejecucion inicializas las variables llamando este método, antes de ejecutar cualquier operación con estas variables
public void initData(){
     dataBase = new MyDataBase(context);
     itemsImportantes = dataBase.getAllDataImportantes(); 
}

PD: If you are using context to call the superclass, make sure that in the MyDatabase constructor it is not null.

    
answered by 23.11.2018 / 15:54
source