I am getting null pointer exception while inserting values into sqlite database. The dh in DatabaseHelper dh = new DatabaseHelper(this)
is coming out to be null when I try to check its value by putting a breakpoint. Below is the code of my DatabaseHelper class and the class from where I am trying to insert the values.
DatabaseHelper Class:
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME="game.db";
public static final String USER_ID="userId";
public static final String STATUS="status";
public static final String MINES="mines";
public static final String OPENTILE="openTile";
public DatabaseHelper(Context context)
{
super(context,DATABASE_NAME,null,1);
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
//CREATE TABLE minesweeper(_id INTEGER PRIMARY KEY AUTOINCREMENT,userId TEXT
arg0.execSQL("CREATE TABLE minesweeper(_id INTEGER PRIMARY KEY AUTOINCREMENT,userId TEXT,status TEXT, mines TEXT, openTile TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS minesweeper");
onCreate(db);
}
}
Main Class:
package com.app.game;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class AppMain extends Activity implements OnClickListener {
String intentString;
DatabaseHelper dha;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dha = new DatabaseHelper(this);
//insertMinesweeper(intentString);
}
void insertMinesweeper(String userId, String status, String mines, String openTile) {
Log.d("","Inside Database function");
Log.d("","Done with database helper");
Log.d("", status);
Log.d("", mines);
Log.d("", openTile);
try
{
//DatabaseHelper dh = new DatabaseHelper(this);
SQLiteDatabase db = dha.getWritableDatabase();
Log.d("","Done with database Sqlite");
ContentValues cv = new ContentValues();
Log.d("","Database connected");
cv.put(DatabaseHelper.USER_ID, intentString);
cv.put(DatabaseHelper.STATUS, status);
cv.put(DatabaseHelper.MINES, mines);
cv.put(DatabaseHelper.OPENTILE, openTile);
db.insert("minesweeper", DatabaseHelper.USER_ID, cv);
db.close();
Log.d("","Values inserted"); }
catch(SQLException e)
{
e.printStackTrace();
}
catch(NullPointerException e) {
e.printStackTrace();
}
// TODO Auto-generated method stub
}
}
}
insertMinesweeper() is being called from another class.
Please help. Thanks in advance.
onCreate
. That may be the source of the problem. – Joseph Earl Mar 24 '11 at 0:42