Click here to Skip to main content
15,886,060 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Java
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);
			// 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);
		}
	}
	
	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

Java
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

Java
//cek tugas
		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);
			}
		});


Please help me, thanks...
Posted

1 solution

In
public void cekPosisi(String txtAlamat)

instead of

dbHandler.closeConn();
rs.close();

try

rs.close();
dbHandler.closeConn();


and if this doesn't work can you paste logcat error line number
 
Share this answer
 
Comments
mbredelz 3-Apr-13 23:45pm    
I still have the same error at code dbHandler.openRead(getApplicationContext());

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900