我在android中打开数据库sqlite时遇到问题。我在打开数据库的代码上遇到此错误"Caused by: java.lang.NullPointerException"
。请帮我纠正一下......
我更新了代码。现在我将代码分开。 DBHelper类
public class DBHelper extends SQLiteOpenHelper {
public static final String DBName = "db_reminder.db";
public static final String TableName = "task_table";
public static final String ID = "id";
public static final String LATITUDE = "latitude";
public static final String LONGITUDE = "longitude";
public static final String RADIUS = "radius";
public static final String ALAMAT = "alamat";
public static final String KONTEKS_TUGAS = "konteks_tugas";
public static final String makeTable = "create table "
+ TableName + "("
+ ID + " integer primary key autoincrement,"
+ LATITUDE + " integer not null,"
+ LONGITUDE + " integer not null,"
+ RADIUS + " integer not null,"
+ ALAMAT + " varchar(50) not null,"
+ KONTEKS_TUGAS + " varchar(50) not null"
+ ");";
public DBHelper(Context context) {
super(context, DBName, null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
Log.d("db", "create table");
db.execSQL(makeTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.d("db", "upgrade table");
db.execSQL("drop table if exists " + TableName);
}
}
DBReminder类
public class DBReminder {
private SQLiteDatabase db;
private DBHelper helper;
private Context konteks;
public DBReminder(Context c) {
this.konteks = c;
helper = new DBHelper(c);
}
public void openWrite() throws SQLException {
db = helper.getWritableDatabase();
}
public void openRead() throws SQLException {
db = helper.getReadableDatabase();
}
public void closeConn() {
helper.close();
}
public void insertTask(Task tugas) {
openWrite();
ContentValues val = new ContentValues();
val.put(helper.LATITUDE, tugas.getLatitude());
val.put(helper.LONGITUDE, tugas.getLongitude());
val.put(helper.RADIUS, tugas.getRadius());
val.put(helper.ALAMAT, tugas.getAlamat());
val.put(helper.KONTEKS_TUGAS, tugas.getKonteks());
db.insert(helper.TableName, null, val);
closeConn();
}}
活动中的代码
public void cekPosisi(String txtAlamat) {
if (txtAlamat.toString().length() > 0) {
dbHandler.openRead();
String sql = "select * from task_table where alamat = '"+txtAlamat+"';";
Cursor rs = db.rawQuery(sql, null);
if(rs != null) {
rs.moveToFirst();
String addr = rs.getString(rs.getColumnIndex("alamat"));
Toast.makeText(this, "Anda ada tugas di " + addr, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Anda tidak ada tugas di lokasi sekarang", Toast.LENGTH_LONG).show();
}
rs.close();
dbHandler.closeConn();
} else {
Toast.makeText(this, "Tidak ada alamat", Toast.LENGTH_LONG).show();
}
}
我在
中使用txtAddr = teksAlamat.getText().toString();
this.cariTugas.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("alamat textView", txtAddr);
cekPosisi(txtAddr);
}
});
我更新了代码,但仍然是同样的错误nullException。请帮忙。
答案 0 :(得分:1)
我真的建议您将openRead()
方法签名更改为:
public DBReminder openRead(Context c) throws SQLException {
helper = new DBHelper(c);
db = helper.getReadableDatabase();
return this;
}
然后
db = dbHandler.openRead(YourActivity.this);
String sql = "select * from task_table where alamat = '"+txtAlamat+"';";
Cursor rs = db.rawQuery(sql, null);
因为您的问题很可能与Context
分配给 NULL
有关。