Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Android Sqlite
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);
			// 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
 
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
 
//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 3-Apr-13 5:34am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
mbredelz at 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)

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 Zoltán Zörgő 99
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 3 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100