Search code examples
androideclipsesqlitenullpointerexceptiongetwritabledatabase

Error in changing database to getWritableDatabase() method


I have source code that use the database.
My database codes are in a class and when I insert anything from this class there is no problem. But if the method call in the other class the error message " java.lang.nullpointerexception " will be shown from this line SQLiteDatabase sd = getWritableDatabase();

This is my main class :

public static class SchemaHelper extends SQLiteOpenHelper {
         private static final String DATABASE_NAME = "adv_data.db";
         // TOGGLE THIS NUMBER FOR UPDATING TABLES AND DATABASE
         private static final int DATABASE_VERSION = 1;
         SchemaHelper(Context context) {
         super(context, DATABASE_NAME, null, DATABASE_VERSION);
         }

         @Override
         public void onCreate(SQLiteDatabase db) {

            // CREATE JOINT TABLE
             db.execSQL("CREATE TABLE"+ JointTable.TABLE_NAME  
            + "("+ JointTable.ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"         
            + JointTable.NAME +" TEXT, "
            + JointTable.FAMILY + " TEXT,"
            + JointTable.ADDRESS+"TEXT,"
            + JointTable.PARTICIPATIONNUM +"Text,"
            + JointTable.RECOGNITIONNUM + "TEXT,"
            + JointTable.USAGE +"TEXT );");


         }
         @Override
         public void onUpgrade(SQLiteDatabase db, int oldVersion,
             int newVersion) {
             Log.w("LOG_TAG", "Upgrading database from version "
             + oldVersion + " to " + newVersion + ", which will destroy all old data");
             // KILL PREVIOUS TABLES IF UPGRADED
             db.execSQL("DROP TABLE IF EXISTS "+ JointTable.TABLE_NAME);

             // CREATE NEW INSTANCE OF SCHEMA
             onCreate(db);
                        } // End of onUpdate class

=============================== INSERT To Table ===================================

         public  long addJoit(String name, String family, String address,
                 String participationNum ,String recognitionNum, String usage ){

             ContentValues cv= new ContentValues();
             cv.put(JointTable.NAME, name);
             cv.put(JointTable.FAMILY, family);
             cv.put(JointTable.ADDRESS, address);
             cv.put(JointTable.PARTICIPATIONNUM, participationNum);
             cv.put(JointTable.RECOGNITIONNUM,recognitionNum);
             cv.put(JointTable.USAGE, usage);

             SQLiteDatabase sd = this.getWritableDatabase();// this line get the error
             long result= sd.insert(JointTable.TABLE_NAME,
                     JointTable.NAME, cv);
             return result;
         }





             }
}

this is the other class that call the method

MainActivity.SchemaHelper sh = new MainActivity.SchemaHelper(null);

long result=  sh.addJoit("name", "family", "address", "participationNum", "recognitionNum", "usage");

I want to know the reason thank's


Solution

  • My problem has solved by calling the method in this way :

    MainActivity.SchemaHelper sh = new MainActivity.SchemaHelper(getBaseContext());
    

    The constructor need getBaseContext() method.

    Thanks to all of you to helped me.