0

I have a problem with opening database sqlite in android. I got this error "Caused by: java.lang.NullPointerException" on code that i open the database. Please help me correct this...

I updated the code. Now i separate the code. The DBHelper class

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);
}
}

The DBReminder class

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();
}}

Code in activity

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();
    }
}

And i use in

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);
        }
    });

I updated the code but still same error nullException. Please help.

6
  • 5
    Which line gives the null pointer? Commented Apr 3, 2013 at 10:51
  • you should put the logcat also so some one could help you Commented Apr 3, 2013 at 10:54
  • and have you initialized konteks (context) Commented Apr 3, 2013 at 10:55
  • in this part dbHandler.openRead(); Commented Apr 3, 2013 at 11:19
  • @AviKumarManku yes i have initialized Commented Apr 3, 2013 at 13:15

1 Answer 1

1

Update:

I really recommend you change your openRead() method signature to:

public DBReminder openRead(Context c) throws SQLException {
   helper = new DBHelper(c);
   db = helper.getReadableDatabase();
   return this;
}

and then

db = dbHandler.openRead(YourActivity.this);
String sql = "select * from task_table where alamat = '"+txtAlamat+"';";
Cursor rs = db.rawQuery(sql, null);

Because your problem is most likely related that your Context is assigned to NULL.

Sign up to request clarification or add additional context in comments.

15 Comments

Should i add it to this function to: public DBReminder openWrite() throws SQLException { helper = new DBHelper(konteks); db = helper.getWritableDatabase(); return this; }
@user2214226 for openWrite() method same, openWrite(Context c)
You mean this: dbHandler.openRead(); it's in activity for query that i posted
It doesn't work too. I code the openWrite() not using SQLiteDatabase. But when i try to use it, it's give me a same error
@IvanAhmed you are doing it wrong simply said. look again at my asnwer and do same like I. you need to call db = dbHandler.openRead(YourActivity.this);
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.