Reputation: 411
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
Upvotes: 0
Views: 960
Reputation: 411
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.
Upvotes: 1
Reputation: 74
Make the different class for simplicity.
public class Insert {
private Context context;
private SchemaHelper schemaHelper;
private SQLiteDatabase database;
public Insert(Context context) {
this.context = context;
}
public long addJoit(String name, String family, String address,
String participationNum ,String recognitionNum, String usage ){
schemaHelper=new SchemaHelper(context);
database=schemaHelper.getWritableDatabase();
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);
long result= database.insert(JointTable.TABLE_NAME,
null, cv);
database.close();
return result;
}
}
In MainActivtity class,
Insert insert=new Insert(MainActivity.this);
long add=insert.addJoit("name", "family", "address", "participationNum", "recognitionNum", "usage");
Also if Table is not created, add spaces in sql statement. ie
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 ); ");
Upvotes: 0
Reputation: 14398
It seems that sh
is null because you pass null in SchemaHelper
constructor,So change
MainActivity.SchemaHelper sh = new MainActivity.SchemaHelper(null);
to
SchemaHelper sh = new SchemaHelper(MainActivity.this);
And also add space in your create table statment,.i.e. Change
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 );");
to
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);");
And also set context
properly ,i.e. change
SchemaHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
to
Context mContext;
SchemaHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
mContext = context;
}
and change
SQLiteDatabase sd = this.getWritableDatabase();
to
SQLiteDatabase sd = mContext.getWritableDatabase();
Upvotes: 0