Query returns null Pointer exception

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;

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

//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);
             }catch (Exception e1){
                 Log.e("DATABAZA","hola "+e1);
                 msg.setText("No Hello you creep");



And my database OnCreate is

public void onCreate(SQLiteDatabase db) {
                + KEY_UID + " INTEGER PRIMARY KEY,"
                + KEY_NAME + " TEXT,"
                + KEY_LAST + " TEXT,"
                + KEY_EMAIL + " TEXT UNIQUE,"
                + KEY_USERNAME + " TEXT UNIQUE" + ")";



Thanks in advance



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!

Need Your Help

Using Cassandra for time series data

cassandra time-series composite-key

I'm on my research for storing logs to Cassandra.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.