I am trying to save data with database on android.
I have wrote class that handle the database, like I saw in several examples.
public class MyDatabase extends SQLiteOpenHelper
{
private Table users;
public MyDatabase(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db)
{
users.CreateTable();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
}
}
But when I try to initialize the variable users, like that:
users = new Table(getWritableDatabase());
I get NullPointerException, and its not meter where… I tryied:
public MyDatabase(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
users = new Table(getWritableDatabase());
}
And
public void onCreate(SQLiteDatabase db)
{
users = new Table(getWritableDatabase());
users.CreateTable();
}
But i still get NullPointerException when I call to the function getWritableDatabase().
I must that the function CreateTable will be not static, because it puts the initial values.
Where I was wrong?
Replace
by
When you’re in onCreate, your writable database is in the process of getting created.
The onCreate method here is not the same as that in your activity. onCreate for SQLiteOpenHelper is called just the first time the DB is created.