我有使用数据库的源代码
我的数据库代码在一个类中,当我从这个类插入任何东西时没有问题。
但是如果方法在另一个类中调用,则会在此行显示错误消息“java.lang.nullpointerexception”SQLiteDatabase sd = getWritableDatabase();
这是我的主要课程:
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;
}
}
}
这是调用方法的另一个类
MainActivity.SchemaHelper sh = new MainActivity.SchemaHelper(null);
long result= sh.addJoit("name", "family", "address", "participationNum", "recognitionNum", "usage");
我想知道原因 感谢的
答案 0 :(得分:1)
通过这种方式调用方法解决了我的问题:
MainActivity.SchemaHelper sh = new MainActivity.SchemaHelper(getBaseContext());
构造函数需要getBaseContext()
方法。
感谢大家的帮助。
答案 1 :(得分:0)
似乎sh
为null,因为您在SchemaHelper
构造函数中传递了null,因此更改
MainActivity.SchemaHelper sh = new MainActivity.SchemaHelper(null);
到
SchemaHelper sh = new SchemaHelper(MainActivity.this);
并在create table语句中添加空格,.i.e。变化
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 );");
到
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);");
并正确设置context
,即。变化
SchemaHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
到
Context mContext;
SchemaHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
mContext = context;
}
并更改
SQLiteDatabase sd = this.getWritableDatabase();
到
SQLiteDatabase sd = mContext.getWritableDatabase();
答案 2 :(得分:0)
为了简单起见,制作不同的课程。
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;
}
}
在MainActivity类中,
Insert insert=new Insert(MainActivity.this);
long add=insert.addJoit("name", "family", "address", "participationNum", "recognitionNum", "usage");
如果未创建Table,请在sql语句中添加空格。即
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 ); ");