java数据库错误 空指针异常 求指教
1.groupDao相关代码:privategroupDbHelperdbHelper;privateStringgroupDb="groupDB.db";private...
1.groupDao相关代码:
private groupDbHelper dbHelper;
private String groupDb="groupDB.db";
private String groupTable="groups";
private String groupThreadTable="groupThread";
private groupDao(Context ctx){
this.ctx=ctx;
dbHelper=new groupDbHelper(ctx, groupDb,1);
}
public static synchronized groupDao getInstance(Context ctx){
if(instance!=null){
}else{
instance=new groupDao(ctx);
}
return instance;
}
public Cursor getAllGroup(){
System.out.println("dbhelper:"+dbHelper);
Cursor cursor=null;
SQLiteDatabase db=dbHelper.getReadableDatabase(); //此行一直在报空指针,但 dbHelper非空 ,为什么?!
cursor=db.query(groupTable, null, null, null, null, null, null);
cursor.setNotificationUri(ctx.getContentResolver(), uri);
return cursor;
}
2.groupDbHelper相关代码:
public class groupDbHelper extends SQLiteOpenHelper {
public groupDbHelper(Context context, String name,
int version) {
super(context, name, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table groups(_id integer primary key autoincrement,group_name varchar(20));");
db.execSQL("create table groupThread(_id integer primary key autoincrement,group_id integer ,thread_id integer);");
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
}
} 展开
private groupDbHelper dbHelper;
private String groupDb="groupDB.db";
private String groupTable="groups";
private String groupThreadTable="groupThread";
private groupDao(Context ctx){
this.ctx=ctx;
dbHelper=new groupDbHelper(ctx, groupDb,1);
}
public static synchronized groupDao getInstance(Context ctx){
if(instance!=null){
}else{
instance=new groupDao(ctx);
}
return instance;
}
public Cursor getAllGroup(){
System.out.println("dbhelper:"+dbHelper);
Cursor cursor=null;
SQLiteDatabase db=dbHelper.getReadableDatabase(); //此行一直在报空指针,但 dbHelper非空 ,为什么?!
cursor=db.query(groupTable, null, null, null, null, null, null);
cursor.setNotificationUri(ctx.getContentResolver(), uri);
return cursor;
}
2.groupDbHelper相关代码:
public class groupDbHelper extends SQLiteOpenHelper {
public groupDbHelper(Context context, String name,
int version) {
super(context, name, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table groups(_id integer primary key autoincrement,group_name varchar(20));");
db.execSQL("create table groupThread(_id integer primary key autoincrement,group_id integer ,thread_id integer);");
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询