简体   繁体   中英

android sqlite cursor object null while fetching a record

I've following dbadapter class, whenever i try to delete a record from the database or fetch any particular contact cursor objet is returned null everytime. anyone know why ?

my code snippets are

dbadapter.java:

public class DBAdapter {
public static final String KEY_ROWID ="_id";
public static final String KEY_NAME ="name";
//public static final String KEY_EMAIL ="email";
public static final String KEY_STUID ="stuid";
public static final String KEY_PASS ="password";
public static final String KEY_SEM ="sem";
public static final String KEY_YEAR ="year";
public static final String KEY_TYPE="type";
public static final String KEY_NOTIFY="notify";
private static final String TAG ="DBAdapter";
private static final String DATABASE_NAME ="MyDB";
private static final String DATABASE_TABLE ="contacts";
private static final int DATABASE_VERSION = 3;
private static final String DATABASE_CREATE ="create table contacts (_id integer primary key autoincrement,"+"name text not null, stuid text not null, password text not null, sem text not null, year text not null, type text not null, notify text not null);";
private Context context = null;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
    this.context=ctx;
    DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
    public DatabaseHelper(Context context)
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db)
    {
        try {
            db.execSQL(DATABASE_CREATE);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        Log.w(TAG,"Upgrading database from version"+oldVersion+"to"+newVersion+", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS contacts");
        onCreate(db);
    }
}
//---opens the database---
public DBAdapter open() throws SQLException
{
    db = DBHelper.getWritableDatabase();
    return this;
}
//---closes the database---
public void close()
{
    DBHelper.close();
}
//---insert a contact into the database---
public long insertContact(String name, String stuid , String password , String sem , String year , String type , String notify)
{
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_NAME, name);
    initialValues.put(KEY_STUID, stuid);
    initialValues.put(KEY_PASS, password);
    initialValues.put(KEY_SEM, sem);
    initialValues.put(KEY_YEAR, year);
    initialValues.put(KEY_TYPE, type);
    initialValues.put(KEY_NOTIFY, notify);
    return db.insert(DATABASE_TABLE, null, initialValues);
}
//---deletes a particular contact---
public boolean deleteContact(long rowId)
{

    return db.delete(DATABASE_TABLE, KEY_ROWID +"="+ rowId, null) > 0;
}
//---retrieves all the contacts---
public Cursor getAllContacts()
{
    return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
            KEY_STUID, KEY_PASS, KEY_SEM, KEY_YEAR, KEY_TYPE, KEY_NOTIFY}, null, null, null, null, null);

}
//---retrieves a particular contact---
public Cursor getContact(long rowId) throws SQLException
{
    Cursor mCursor =

            db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
                    KEY_NAME, KEY_STUID, KEY_PASS, KEY_SEM, KEY_YEAR, KEY_TYPE, KEY_NOTIFY}, KEY_ROWID +"=" + rowId, null,
        null, null, null, null);
    if (mCursor != null && mCursor.moveToFirst()) {

        return mCursor;
    }
    else {
        mCursor.close();
    }
    return null;
}



//---updates a contact---
public boolean updateContact(long rowId, String name,  String stuid , String password , String sem , String year , String type , String notify)
{
    ContentValues args = new ContentValues();
    args.put(KEY_NAME, name);
    args.put(KEY_STUID, stuid);
    args.put(KEY_PASS, password);
    args.put(KEY_SEM, sem);
    args.put(KEY_YEAR, year);
    args.put(KEY_TYPE, type);
    args.put(KEY_NOTIFY, notify);
    return db.update(DATABASE_TABLE, args, KEY_ROWID +"="+ rowId, null) > 0;
}
}

I am calling the method from listview in mainactivity.java as

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Toast.makeText(MainActivity.this,"You clicked at "+position,Toast.LENGTH_SHORT).show();

            try {
                Cursor cursor1;
                cursor1 = dbhelper.getContact(position + 1);


                Toast.makeText(MainActivity.this, "id=" + cursor1.getString(2) + " pass=" + cursor1.getString(3) + " session=" + cursor1.getString(4) + " year=" + cursor1.getString(5) + " type=" + cursor1.getString(6), Toast.LENGTH_SHORT).show();
            }

            catch (Exception e)
            {
                Toast.makeText(MainActivity.this,""+e,Toast.LENGTH_LONG).show();
            }
}

which generates null pointer exception. please help.

Note : getallcontacts() and insertcontact() methods work fine.

Try this in your getContact Method;

if (mCursor != null && mCursor.moveToFirst()) 
{
String val1 = mCursor.getString("ColumnName1"); 
String val2 = mCursor.getString("ColumnName2");
// Check if Expected Record is returned
Toast.make(Context, val1+val2, Toast.LONG).show();
return mCursor;
}
else
{
    return mCursor =null;
}
// Close Cursor after Retrieving Values from it. 
if (!mCursor.close())
{
mCursor.close(); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM