4

I'm trying to read data out of a database I created, but I always get a NullPointerException when trying to fetch all my records.

I practically copypasted the code from another application I have running perfectly, but somehow I'm doing it wrong here.

The NullPointerException is at the line of

return mDb.query(DATABASE_TABLE_LOCALLOGIN, new String[] {LOCALLOGIN_ID, LOCALLOGIN_LOGIN, LOCALLOGIN_PASSWORD}, null, null, null, null, null);

Here's the relevant code (don't mind the string arrays, its for adding tables later on: 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, where I call 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();

}
}
0

2 Answers 2

11

You'll probably want to call the open() method before you make the query:

//...
mDbHelper.open(); //whitout this call mdb will be NULL
Cursor localLogin = mDbHelper.fetchAllLocalLogins();
Sign up to request clarification or add additional context in comments.

3 Comments

Did that, now I get "android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1" at "mLoginField.setText(localLogin.getString(localLogin.getColumnIndexOrThrow(GoingOutDbAdapter.LOCALLOGIN_LOGIN)));" with mLoginField being an EditText in my contentview
@MatsRaemen The Cursor that you are returning from a query is positioned at -1 so you should always call localLogin.moveToFirst() or localLogin.moveToNext() before you attempt to get data.
Its easy to forget to open it... I built a helper class that opens in the constructor :)
1

You should open the database before performing the database operation. mDbHelper.open();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.