1

I have to read values from SQLite database but getting NullPointerException when I execute

   private final String DB_NAME = "UserDb";
   private final String TABLE_NAMES = "tbluser"; 
   SQLiteDatabase sampleDB;
   Cursor c=null;
   String deviceDec;  
   c= sampleDB.rawQuery("SELECT * from " +TABLE_NAMES+" where did='"+deviceDec+"' ",null);

Please suggest me why I am getting the exception because in one function it is running but in other it is not and also I am getting the deviceDec value that I have checked in Log.

3
  • please paste your table structure. Commented Oct 31, 2012 at 5:01
  • this is how i am creating the database sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +TABLE_NAME + " (id INTEGER PRIMARY KEY, did TEXT, idate DATE, edate DATE)"); sampleDB.execSQL("INSERT INTO " +TABLE_NAME +"(did,idate,edate) Values ('"+IMEI+"','"+fDate+"','"+feDate+"');"); Commented Oct 31, 2012 at 5:07
  • but the problem is In the activity where i am creating the database is working file but in other activity i have to just read the database there it is throwing null pointer exception Commented Oct 31, 2012 at 5:09

3 Answers 3

1

May be your sampleDB; is null.

so first you have init that object like as

SQLiteDatabase sampleDB = new SQLiteDatabase(Your Param);
Sign up to request clarification or add additional context in comments.

Comments

1

you need to initiate DB..

SQLiteDatabase sampleDB=context.getReadableDatabase();

Comments

1

open database before selecting data from it.

public SQLiteDatabase openDataBase() throws SQLException {
    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READWRITE);

    return myDataBase;
}

this will return dataabase.. and then use returnedDb.rawQuery("");

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.