-1

I am using public class DatabaseHandler that extends SQLiteOpenHelper and this is my method to get all the data from the db, it looks all fine but crashes, and activity says nullpointerException. Is there anything to change in the code? I have only one row of information..

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

In, Account Activity,

db = new DatabaseHandler(AccountActivity.this);

   // Declared all String

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

and set those String to TextView as required..

but it says nullpointer exception.

Kailas
  • 7,350
  • 3
  • 47
  • 63
Vinnig
  • 7
  • 4
  • if (cursor.getCount !=0) { while(cursor.moveToNext()){ 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); } } } – user8938 Jun 11 '14 at 11:38
  • 1
    @Vinayak Nonsense. `while(cursor.moveToNext())` is enough. No need count, no need if/do/while like OP. – m0skit0 Jun 11 '14 at 11:41
  • : FATAL EXCEPTION: main : Process: com.vinnig.loginandplay, PID: 1022 : java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vinnig.loginandplay/com.vinnig.pages.AccountActivity}: java.lang.NullPointerException 06-11 07:36:25.914: E/AndroidRuntime(1022): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) : at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 06-11 07:36:25.914: E/AndroidRuntime(1022): at android.app.ActivityThread.access$800(ActivityThread.java:135) – Vinnig Jun 11 '14 at 11:50
  • its not duplicate, it has different issue – Vinnig Jun 11 '14 at 11:50
  • Thanks, Vinayak.. but still the same error, as logcat above.. – Vinnig Jun 11 '14 at 11:51
  • There are problems in the code you posted but not necessarily anything to do with the NPE (for example, the `getData()` doesn't do anything useful as the retrieved data is thrown away). Start by editing the post and posting the full exception stacktrace (including the nested NPE itself). – laalto Jun 11 '14 at 11:57
  • ok, how to post full exception stacktrace.. because it does not let me post.. – Vinnig Jun 11 '14 at 12:11

2 Answers2

0

Brothers, I found that the method to getData is all wrong.. The Right Method is..

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

means No parameters in a method, like i did before, because we are not adding or updating any fields, so no need for that in case of fetching data..

And in Activity,

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;

name, email, password are Strings inside and you can set it respectively.. Thanks for your help..

Vinnig
  • 7
  • 4
-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
            }
        }