J'essaie de lire les données d'une base de données que j'ai créée, mais j'obtiens toujours une NullPointerException lorsque j'essaie de récupérer tous mes enregistrements.
J'ai pratiquement copié le code d'une autre application qui fonctionne parfaitement, mais je ne sais pas comment je m'y prends.
L'exception NullPointerException se trouve à la ligne de
return mDb.query(DATABASE_TABLE_LOCALLOGIN, new String[] {LOCALLOGIN_ID, LOCALLOGIN_LOGIN, LOCALLOGIN_PASSWORD}, null, null, null, null, null);
Voici le code correspondant (ne faites pas attention aux tableaux de chaînes, c'est pour ajouter des tableaux plus tard) : GoingOutDbAdapter.java
public class GoingOutDbAdapter {
private static final String DATABASE_NAME = "GoingOutData";
private static final String DATABASE_TABLE_LOCALLOGIN = "LocalLogin";
public static final String LOCALLOGIN_ID = "LocalLogin_id";
public static final String LOCALLOGIN_LOGIN = "Login";
public static final String LOCALLOGIN_PASSWORD = "Password";
private static final String TAG = "Debugstring";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String[] DATABASE_CREATE = {
"CREATE Table " + DATABASE_TABLE_LOCALLOGIN + " ( "
+ LOCALLOGIN_ID + " integer PRIMARY KEY Autoincrement, "
+ LOCALLOGIN_LOGIN + " text NOT NULL, "
+ LOCALLOGIN_PASSWORD + " text NOT NULL );"};
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
for(int i = 0; i < DATABASE_CREATE.length; i++){
Log.d(TAG,DATABASE_CREATE[i]);
db.execSQL(DATABASE_CREATE[i]);
}
}
}
public GoingOutDbAdapter(Context ctx) {
this.mCtx = ctx;
}
public GoingOutDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
public Cursor fetchAllLocalLogins() {
return mDb.query(DATABASE_TABLE_LOCALLOGIN, new String[] {LOCALLOGIN_ID, LOCALLOGIN_LOGIN, LOCALLOGIN_PASSWORD}, null, null, null, null, null);
}
}
MyActivity.java, où j'appelle fetchAllLocalLogins
public class MyActivity extends Activity {
/** Called when the activity is first created. */
private GoingOutDbAdapter mDbHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDbHelper = new GoingOutDbAdapter(this);
setContentView(R.layout.main);
Cursor localLogin = mDbHelper.fetchAllLocalLogins();
}
}