从android中的SQLiteDatabase获取数据时获取NullPointerException

时间:2014-06-11 11:31:44

标签: java android sqlite android-sqlite

我正在使用扩展SQLiteOpenHelper的公共类DatabaseHandler 这是我从db获取所有数据的方法,它看起来很好但崩溃,并且活动说nullpointerException。代码中有什么要改变的吗?我只有一行信息..

// Getting all data

public Cursor getData(String UserName, String Email, String Password,
        String BirthDate, String PinCode, String Country, String PaypalID,
        String mobile, String FBID) {

    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_DATA, new String[] { "UserName",
            "Email", "Password", "BirthDate", "PinCode", "Country",
            "PaypalID", "mNUMBER", "FBID" }, null, null, null, null, null,
            null);

    if (cursor != null) {

        if (cursor.moveToFirst()) {
            do {
                Log.i("counttttttttttttt", "" + cursor.getCount());

                UserName = cursor.getString(0);
                Email = cursor.getString(1);
                Password = cursor.getString(2);
                BirthDate = cursor.getString(3);
                PinCode = cursor.getString(4);
                Country = cursor.getString(5);
                PaypalID = cursor.getString(6);
                mobile = cursor.getString(7);
                FBID = cursor.getString(8);

            } while (cursor.moveToNext());
        }
    }

    db.close();
    return cursor;
}

在,帐户活动,

db = new DatabaseHandler(AccountActivity.this);

   // Declared all String

    db.getData(name, email, password, birthdate, pincode, country, paypal,
            mobile, fbid);

并根据需要将这些String设置为TextView ..

但它表示nullpointer异常。

2 个答案:

答案 0 :(得分:0)

兄弟们,我发现getData的方法都错了.. 正确的方法是......

// Getting all data
public Cursor getData() {

    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_DATA, new String[] { "UserName",
            "Email", "Password", "BirthDate", "PinCode", "Country",
            "PaypalID", "mNUMBER", "FBID" }, null, null, null, null, null,
            null);

    return cursor;
}

表示方法中没有参数,就像我之前做的那样,因为我们没有添加或更新任何字段,因此在获取数据时不需要这样做..

在活动中,

DatabaseHandler db = new DatabaseHandler(AccountActivity.this);

    // getAllData();
    Cursor cursor = db.getData();

if (cursor.moveToFirst()) {
        do {
            Log.i("counttttttttttttt", "" + cursor.getCount());

            name = cursor.getString(0);
            email = cursor.getString(1);
            password = cursor.getString(2);
            birthdate = cursor.getString(3);
            pincode = cursor.getString(4);
            country = cursor.getString(5);
            paypal = cursor.getString(6);
            mobile = cursor.getString(7);
            fbid = cursor.getString(8);

        } while (cursor.moveToNext());
    }
db.close;

名称,电子邮件,密码都是字符串,你可以分别设置它。 谢谢你的帮助......

答案 1 :(得分:-1)

            if( cursor !=null && cursor.getCount() != 0)
        {
            while( cursor.moveToNext() )
            {
                UserName = cursor.getString(0);
                Email = cursor.getString(1);
                Password = cursor.getString(2);
                BirthDate = cursor.getString(3);
                PinCode = cursor.getString(4);
                Country = cursor.getString(5);
                PaypalID = cursor.getString(6);
                mobile = cursor.getString(7);
                FBID = cursor.getString(8);
                //add data to a list
            }
        }