Binoy Babu
Binoy Babu

Reputation: 17139

Can't open SQLite database

I'm trying to pen my SQLite db with:

dbHelper = new DataBaseHelper(context);
public static void open() throws SQLException {
    try {
        database = dbHelper.getWritableDatabase();
    } catch (Exception e) {
        e.printStackTrace();
        Log.d(Tag.getTag("open()"), "Can't open db");
    }
}

But it gives NullPointerException. Following is my DataBaseHelper class.

//declarations and stuff

public DataBaseHelper(Context context) {
    super(context, VFS_DATABASE_NAME, null, DATABASE_VERSION);
    this.context = context;
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    Toast.makeText(context, "Creating DataBase for first time...", 2000).show();
    db.execSQL(DATABASE_CREATE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(DataBaseHelper.class.getName(),
            "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS" + VFS_DATABASE_TABLE);
    onCreate(db);

}

I can't figure out what I'm doing wrong. Why is this error happening?

Stack trace:

02-27 23:16:07.698: W/System.err(4685): java.lang.NullPointerException
02-27 23:16:07.698: W/System.err(4685):     at com.manager.boot.r1223.VSDataSource.open(VSDataSource.java:34)
02-27 23:16:07.698: W/System.err(4685):     at com.manager.boot.r1223.VSDataSource.ScanVirtualSystems(VSDataSource.java:52)
02-27 23:16:07.706: W/System.err(4685):     at com.manager.boot.r1223.OSListActivity.onCreate(OSListActivity.java:49)
02-27 23:16:07.706: W/System.err(4685):     at android.app.Activity.performCreate(Activity.java:4465)
02-27 23:16:07.706: W/System.err(4685):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-27 23:16:07.706: W/System.err(4685):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
02-27 23:16:07.706: W/System.err(4685):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
02-27 23:16:07.706: W/System.err(4685):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
02-27 23:16:07.706: W/System.err(4685):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
02-27 23:16:07.706: W/System.err(4685):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 23:16:07.706: W/System.err(4685):     at android.os.Looper.loop(Looper.java:137)
02-27 23:16:07.706: W/System.err(4685):     at android.app.ActivityThread.main(ActivityThread.java:4424)
02-27 23:16:07.706: W/System.err(4685):     at java.lang.reflect.Method.invokeNative(Native Method)
02-27 23:16:07.706: W/System.err(4685):     at java.lang.reflect.Method.invoke(Method.java:511)
02-27 23:16:07.706: W/System.err(4685):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-27 23:16:07.706: W/System.err(4685):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-27 23:16:07.706: W/System.err(4685):     at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 531

Answers (1)

murko
murko

Reputation: 111

I think the problem is in static. Try without it.

My example:

public class MyActivity extends ListActivity {

   private DatabaseHandler db;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);

    db = new DatabaseHandler(this);
    ...
}

Upvotes: 2

Related Questions