Hi everyone, i have a problem while try to open database in sqlite android. I want to match the data in query with the data from textview but when the app run it force close. I saw the log cat and the method that use for open db is null exception.
This is the complete code of db class
public class DBReminder {
private SQLiteDatabase db;
private DBHelper helper;
private Context konteks;
private static final String DBName = "db_reminder.db";
private 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";
private 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"
+ ");";
private static class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, DBName, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d("db", "create table");
db.execSQL(makeTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.d("db", "upgrade table");
db.execSQL("drop table if exists " + TableName);
}
}
public DBReminder(Context c) {
this.konteks = c;
helper = new DBHelper(c);
}
public DBReminder openWrite(Context c) throws SQLException {
helper = new DBHelper(c);
db = helper.getWritableDatabase();
return this;
}
public DBReminder openRead(Context c) throws SQLException {
helper = new DBHelper(c);
db = helper.getReadableDatabase();
return this;
}
public void closeConn() {
helper.close();
}
public void insertTask(Task tugas) {
openWrite(konteks);
ContentValues val = new ContentValues();
val.put(LATITUDE, tugas.getLatitude());
val.put(LONGITUDE, tugas.getLongitude());
val.put(RADIUS, tugas.getRadius());
val.put(ALAMAT, tugas.getAlamat());
val.put(KONTEKS_TUGAS, tugas.getKonteks());
db.insert(TableName, null, val);
closeConn();
}
}
And i use that in this function
public void cekPosisi(String txtAlamat) {
if (txtAlamat.toString().length() > 0) {
dbHandler.openRead(getApplicationContext());
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();
}
dbHandler.closeConn();
rs.close();
} else {
Toast.makeText(this, "Tidak ada alamat", Toast.LENGTH_LONG).show();
}
}
The function cekPosisi used in
txtAddr = teksAlamat.getText().toString();
this.cariTugas.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d("alamat textView", txtAddr);
cekPosisi(txtAddr);
}
});
Please help me, thanks...