0

Hello I have a problem with a query. I guess I am missing something, but I do not know what, so I need your help

It returns java.lang.NullPointerException

 import android.annotation.TargetApi;
    import android.app.Activity;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.widget.TextView;
    import android.util.Log;

    @TargetApi(16)
    public class logged extends Activity{
        public TextView msg;
        private SQLiteDatabase myDB = null;
        public void onCreate(Bundle savedInstanceState){
             super.onCreate(savedInstanceState);
             setContentView(R.layout.logged);
             msg = (TextView) findViewById(R.id.msg);
             myDB.openDatabase("data/data/com.example.login2/databases/aeglea", null, SQLiteDatabase.OPEN_READONLY);

             try{
//here is the problem. Both kind of queries return the same error
                 //Cursor cur = myDB.query("user_login", new String[] {"username"}, null, null, null, null, null);
                 Cursor cur = myDB.rawQuery("SELECT username FROM user_login", null);
                 msg.setText("Hello");
             }catch (Exception e1){
                 Log.e("DATABAZA","hola "+e1);
                 msg.setText("No Hello you creep");
             }

        }


    }

And my database OnCreate is

public void onCreate(SQLiteDatabase db) {
        String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_NAME + "("
                + KEY_UID + " INTEGER PRIMARY KEY,"
                + KEY_NAME + " TEXT,"
                + KEY_LAST + " TEXT,"
                + KEY_EMAIL + " TEXT UNIQUE,"
                + KEY_USERNAME + " TEXT UNIQUE" + ")";

        db.execSQL(CREATE_LOGIN_TABLE);


    }

Thanks in advance

5
  • I think your path to your database file needs to be absolute: /data/data/com.example.login2/databases/aeglea (note the leading /) Commented Aug 23, 2012 at 18:40
  • 1
    Are you sure myDB is not null and valid after open? Commented Aug 23, 2012 at 18:40
  • @Marco I check this with isOpen() right? Commented Aug 23, 2012 at 18:49
  • @xbakesx I tried. Nothing different :/ Commented Aug 23, 2012 at 18:51
  • @Peter sorry, yeah. The answer below from Sam is right. You never initialized myDB. Commented Aug 23, 2012 at 18:56

1 Answer 1

3

Rewrite

private SQLiteDatabase myDB = null;

mDB is null, you need to initialize it before referencing it.

Try using your extended SQLiteOpenHelper class:

Database openHelper = new Database(this);
myDB = openHelper.getWritableDatabase(); // or getReadableDatabase();

And remove:

myDB.openDatabase("data/data/com.example.login2/databases/aeglea", null, SQLiteDatabase.OPEN_READONLY);

Now mDB.query() should work!

Sign up to request clarification or add additional context in comments.

4 Comments

is this valid? I get the "Change type of myDB to SQLiteOpenHelper" private SQLiteDatabase myDB = null; myDB = new SQLiteOpenHelper(this, "aeglea", null, 1); this gets a weird error "; expected ,"
Sorry, that code is wrong. The advice about the error is correct, but as you noticed a SQLiteOpenHelper is not a SQLiteDatabase. Before I can give you a better answer: do you have a database adapter class? What about an OpenHelper where you define onCreate() for your database?
No I don;t think so. I have a class called Database extends SQLite helper where I first create the database "aeglea". You mean I need to define another class where it opens "aeglea"?
Daamn you are pro. Thanks. I go study to understand what you did :P

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.