0

我在 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。请帮忙。

4

1 回答 1

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.

于 2013-04-03T11:09:45.693 回答