0

Hello folks after a long time I am posting any query here. I am getting a NullPointer Exception Error in the following code:-

public class DBAdapter {

int id = 0;
public static final String KEY_ROWID = "_id";
public static final String KEY_FNAME = "FirstName";
public static final String KEY_LNAME = "LastName";
public static final String KEY_EMAILID = "Email";
public static final String KEY_PASSWORD = "Password";
private static final String TAG = "DBAdapter";

private static final String DATABASE_NAME = "UserDetail";
private static final String DATABASE_TABLE = "tblUserDetail";

private static final int DATABASE_VERSION = 1;

private static final String DATABASE_CREATE =  "create table tblUserDetail (_id integer primary key autoincrement, "
        + "FirstName text not null, LastName text, Email text not null, Password text not null );";

private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public DBAdapter(Context ctx){
    this.context = ctx;
    DBHelper = new DatabaseHelper(ctx);
}

private static class DatabaseHelper extends SQLiteOpenHelper{

    public DatabaseHelper(Context context) {
            super(context,DATABASE_NAME,null,DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
            db.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
            Log.w(TAG, "Upgrading database version from"+ oldVersion +
                    "to"+ newVersion +", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXIST tblUserDetail");
            onCreate(db);
    }
}

    public DBAdapter open() throws SQLException{

        db = DBHelper.getWritableDatabase();
        return this;

    }

    public void close(){
        DBHelper.close();
    }

    public long insertData(String fName,String lName, String emailId, String password){
        ContentValues intialValues = new ContentValues();
        intialValues.put(KEY_FNAME, fName);
        intialValues.put(KEY_LNAME, lName);
        intialValues.put(KEY_EMAILID, emailId);
        intialValues.put(KEY_PASSWORD, password);
        return db.insert(DATABASE_TABLE, null, intialValues);
    }

    public String getAllEntries()
    {
        SQLiteDatabase database = DBHelper.getReadableDatabase();

        Cursor cursor = database.rawQuery(
                    "SELECT FirstName FROM tblUserDetail WHERE _id = 1", null);
                if(cursor.moveToFirst()) {
                    return cursor.getString(0);
                }
                return cursor.getString(0);

    }

}

I am calling insertData and getAllEntries on a button click:-

Submit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
    //      Toast toast = Toast.makeText(getApplicationContext(), "cliecked on submit button", Toast.LENGTH_LONG);
    //      toast.show();

            String str;
            try{
                db.insertData(etFN.getText().toString(), etLN.getText().toString(), etEI.getText().toString(), etPS.getText().toString());

                str = db.getAllEntries();

                Toast toast = Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG);
                toast.show();
            }

            catch(Exception ex){
                 Context context = getApplicationContext();
                 CharSequence text = ex.toString();
                 int duration = Toast.LENGTH_LONG;

                 Toast toast = Toast.makeText(context, text, duration);
                 toast.show();
            }

        } 
    });

Please help me to get rid of this NullPointer Exception Error.Every help will be highly appreciated.

I am getting the error on db.insertData() line.

7
  • Which line is throwing the Exception? Commented Dec 13, 2013 at 11:54
  • Can you paste your stacktrace ? We need see what exactly is throwing this Exception. Commented Dec 13, 2013 at 11:56
  • 2
    please post your logcat error Commented Dec 13, 2013 at 11:56
  • Getting NullPointer Exception ok. but where? Commented Dec 13, 2013 at 11:57
  • @himanshu If want faster solution then please post your logcat. Commented Dec 13, 2013 at 12:01

1 Answer 1

1

It happens may be with two reasons.

1) If you have not initialized your DBADapter object. You have just declared it.

so initialize it

 db = new DBAdapter(this);

2) You have not open or close your database before insert data into database. So first open your database before insert data into it.

Thanks!!

Sign up to request clarification or add additional context in comments.

Comments

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.