I'm having a problem with a class I've done in Android Studio that extends from SQLiteOpenHelper
. The problem is this: I have created a class that extends from SQLiteOpenHelper
that handles a table from a database. I have already created more databases with this library and I have not had any problems so far. In this class it seems that it does not create the database or the table, I tried to change the activity in case it was the context, but it seems that this is not the problem, I have also reviewed the query of the CREATE TABLE run without errors and does it without problem. The error he gives me is this:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
And it gives me in the following line of code of the method getIdsLugares()
, which is the first one that executes when I have already created and initialized the class of the database and I want to consult some data:
SQLiteDatabase db=getReadableDatabase();
Then I leave the class code. I hope you can help me because I've tried everything and I do not know what it can be. Greetings and thanks.
public class BDLugares extends SQLiteOpenHelper{
static String DB_NAME="mijaresapp";
static final String TABLE_NAME="lugares";
static final String[] COLUMNAS={"idcategoria","categoria","descripcion","direccion","imagen","pathimagen","lat","lon","ruta","titulo","id"};
String queryCreation="CREATE TABLE "+TABLE_NAME+" ("+COLUMNAS[0]+" TEXT, "+COLUMNAS[1]+" TEXT, "+COLUMNAS[2]+
" TEXT, "+COLUMNAS[3]+" TEXT, "+COLUMNAS[4]+" TEXT, "+COLUMNAS[5]+" TEXT, "+COLUMNAS[6]+
" DOUBLE, "+COLUMNAS[7]+" DOUBLE, "+COLUMNAS[8]+" TEXT, "+COLUMNAS[9]+" TEXT, "+ COLUMNAS[10]+" TEXT)";
static final int VERSION_ACTUAL=1;
public BDLugares(Context context) {
super(context, DB_NAME, null, VERSION_ACTUAL);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(queryCreation);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public Cursor getIdsLugares(){
SQLiteDatabase db=getReadableDatabase();
Cursor c=db.rawQuery("SELECT "+COLUMNAS[10]+" FROM "+TABLE_NAME,null);
return c;
}
public long setDatosLugares(ItemCardMap item){
SQLiteDatabase db=getWritableDatabase();
ContentValues c = new ContentValues();
c.put(COLUMNAS[0],item.getParentID());
c.put(COLUMNAS[1],item.getTipo());
c.put(COLUMNAS[2],item.getContenido());
c.put(COLUMNAS[3],item.getDireccion());
c.put(COLUMNAS[4],item.getImagen());
c.put(COLUMNAS[5],item.getPathImagen());
c.put(COLUMNAS[6],item.getLat());
c.put(COLUMNAS[7],item.getLon());
c.put(COLUMNAS[8],item.getRuta());
c.put(COLUMNAS[9],item.getNombre());
c.put(COLUMNAS[10],item.getId());
long n=db.insert(TABLE_NAME,null,c);
return n;
}
public int borrarDatos(String id){
SQLiteDatabase db=getWritableDatabase();
String[] datos={id};
int n=db.delete(TABLE_NAME,COLUMNAS[1]+"=?",datos);
db.close();
return n;
}
public int borrarDatos(ArrayList ids){
String[] datos=new String[ids.size()];
for(int i=0; i<ids.size(); i++){
datos[i]= String.valueOf(ids.get(i));
}
SQLiteDatabase db=getWritableDatabase();
int n=db.delete(TABLE_NAME,COLUMNAS[1]+"=?",datos);
db.close();
return n;
}
public Cursor traerDatosCategoria(String parentID){
SQLiteDatabase db=getReadableDatabase();
String[] datos={parentID};
Cursor c=db.rawQuery("SELECT * FROM "+TABLE_NAME+" WHERE "+COLUMNAS[0]+"=?",datos);
return c;
}
}