0

I tried multiple experiments and realized that I'm getting a NullPointerException when trying to execute a SQL query. My complete query is:

create table transactiontable(_id integer primary key autoincrement, name text not null, reason text not null, money text not null );

Suggestions on where I went wrong? Stack trace is:

Caused by: java.lang.NullPointerException
at com.example.startandroid.DBAdapter$DatabaseHelper.<init>(DBAdapter.java:110)
at com.example.startandroid.DBAdapter.open(DBAdapter.java:43)
at com.example.startandroid.MainActivity.onButtonClick(MainActivity.java:34)
Not sure what is the issue.

My SQL adaptor class is :

public class DBAdapter {

private static final String DATABASE_NAME = "transaction.db";
private static final String DATABASE_TABLE = "transactiontable";
private static final int    DATABASE_VERSION = 1;

public static final String KEY_ID   = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_REASON = "reason";
public static final String KEY_MONEY = "money";

private static final String DATABASE_CREATE = "create table " 
        + DATABASE_TABLE 
        + "("
        + KEY_ID + " integer primary key autoincrement, "
        + KEY_NAME + " text not null,  " 
        + KEY_REASON + " text not null, "
        + KEY_MONEY + " text not null "
        + ");";

private final Context context;

private DatabaseHelper DBHelper;
private static SQLiteDatabase db;

public DBAdapter(Context context)
{
    this.context = context;
}

// Open the database
public DBAdapter open() throws SQLException
{
    DBHelper = new DatabaseHelper(context);
    if (DBHelper != null )
        db = DBHelper.getWritableDatabase();
    return this;
}

// Close the database
public void close()
{
    DBHelper.close();
}


private static class DatabaseHelper extends SQLiteOpenHelper
{
    DatabaseHelper(Context context)
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        db.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onCreate(SQLiteDatabase arg0) 
    {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
    }
}
}

I call this class's function using code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    db = new DBAdapter(this);
    Log.e(TAG, "OnCreate is run");
    myIntent = new Intent(MainActivity.this, FrameActivity.class);
}

public void onButtonClick(View v)
{
    try
    {
        Log.e(TAG, "OnButtonClick starts");
        if (db != null)
        {
            db.open();
            db.close();
        }
    }
    catch(SQLException e)
    {
        e.printStackTrace();
    }
    MainActivity.this.startActivity(myIntent);
    finish();
}
1
  • Are you passing in a valid context to the DBAdapter() contructor? Commented Jul 27, 2012 at 18:22

1 Answer 1

1

db is null in the constructor for the DatabaseHelper. Move the line:

db.execSQL(DATABASE_CREATE);

in the onCreate method of DatabaseHelper:

@Override
public void onCreate(SQLiteDatabase arg0) {
    arg0.execSQL(DATABASE_CREATE);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this worked. I want to know that why db is null after db = new DBAdapter(this) point?
@miteshsharma The db from my answer is not referring to the DBAdapter, is referring to the SQLiteDatabase field in DBAdapter(which you unfortunately named as well, db). When you call db.open() the db SQLiteDatabase field is still null as it isn't pointing to a valid database and calling execSQL() on it will throw the exception.

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.