Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CursorIndexOutOfBoundsException in android

I tried a simple registration application in android using android studio. I am trying to make a simple database and insert values in it. I am getting the following error which i am unable to understand when i debug my app.

FATAL EXCEPTION: main android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

Any suggestions will be highly appreciated. The java code goes here:

FunTube

Class that get and set the values

DatabaseWrapper.java

public class DatabaseWrapper extends SQLiteOpenHelper {
    public static final String FUNTUBE= "User";
    public static final String FUNTUBE_ID = "_id";
    public static final String FUNTUBE_UNAME = "_username";
    public static final String FUNTUBE_EMAIL = "_email";
    public static final String FUNTUBE_PASSWORD = "_password";
    public static final String FUNTUBE_FNAME="_fname";
    public static final String FUNTUBE_LNAME = "_lname";
    public static final String FUNTUBE_PHONE="_phone";
    public static final String FUNTUBE_COUNTRY = "_country";
    public static final String FUNTUBE_GENDER = "_gender";
    public static final String FUNTUBE_VIDEOPATH= "_videopath";
    public static final String FUNTUBE_BDAY = "_bday";
    public static final String FUNTUBE_YEAR = "_year";
    public static final String FUNTUBE_INTEREST = "_interest";
    public static final String FUNTUBE_RELIGION = "_religion";
    public static final String FUNTUBE_ABOUT="_about";
    public static final String FUNTUBE_QUOTE="_qoute";
    public static final String FUNTUBE_JOB = "_job";

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);

    }


    private static final String DATABASE_NAME = "FunTube.db";
    private static final int DATABASE_VERSION = 1;

    // creation SQLite statement
    private static final String DATABASE_CREATE = "create table " +FUNTUBE
            + "(" + FUNTUBE_ID + " integer primary key autoincrement, "

            + FUNTUBE_UNAME + " text not null,"
            +FUNTUBE_EMAIL + " text not null,"
            +FUNTUBE_PASSWORD + " text not null,"
            + FUNTUBE_FNAME + " text not null,"
            +FUNTUBE_LNAME + " text not null,"
            +FUNTUBE_PHONE + " text not null,"
            + FUNTUBE_COUNTRY + " text not null,"
            +FUNTUBE_GENDER + " text not null,"
            +FUNTUBE_VIDEOPATH + " text not null,"
            + FUNTUBE_BDAY + " text not null,"
            +FUNTUBE_YEAR + " text not null,"
            +FUNTUBE_INTEREST + " text not null,"
            + FUNTUBE_RELIGION + " text not null,"
            +FUNTUBE_ABOUT + " text not null,"
            +FUNTUBE_QUOTE + " text not null,"
            +FUNTUBE_JOB+ " text not null)";




    public DatabaseWrapper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }



    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        if(newVersion>oldVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + FUNTUBE);
            onCreate(db);
        }

    }

}

FunTubeOperations

public class FunTubeOperations {
    private DatabaseWrapper dbHelper;
    private String[] FUNTUBE_TABLE_COLUMNS = { DatabaseWrapper.FUNTUBE_ID, DatabaseWrapper.FUNTUBE_UNAME,DatabaseWrapper.FUNTUBE_EMAIL, DatabaseWrapper.FUNTUBE_PASSWORD,DatabaseWrapper.FUNTUBE_FNAME, DatabaseWrapper.FUNTUBE_LNAME,DatabaseWrapper.FUNTUBE_PHONE, DatabaseWrapper.FUNTUBE_COUNTRY,DatabaseWrapper.FUNTUBE_GENDER, DatabaseWrapper.FUNTUBE_VIDEOPATH,DatabaseWrapper.FUNTUBE_BDAY, DatabaseWrapper.FUNTUBE_YEAR,DatabaseWrapper.FUNTUBE_INTEREST, DatabaseWrapper.FUNTUBE_RELIGION,DatabaseWrapper.FUNTUBE_ABOUT, DatabaseWrapper.FUNTUBE_QUOTE, DatabaseWrapper.FUNTUBE_JOB};

    private SQLiteDatabase database;

    public FunTubeOperations(Context context) {
        dbHelper = new DatabaseWrapper(context);
    }

    public void open() throws SQLException {
        database = dbHelper.getWritableDatabase();
    }

    public void close()
    {
        dbHelper.close();
    }

    public FunTube addUsers(String uname,String email,String password,String lname,String fname,String phone,String country,String gender,String videopath,String bday,String year,String interest,String religion,String about,String qoute,String job) {

        ContentValues values = new ContentValues();

        values.put(DatabaseWrapper.FUNTUBE_UNAME, uname);
        values.put(DatabaseWrapper.FUNTUBE_EMAIL, email);
        values.put(DatabaseWrapper.FUNTUBE_PASSWORD, password);
        values.put(DatabaseWrapper.FUNTUBE_FNAME, fname);
        values.put(DatabaseWrapper.FUNTUBE_LNAME, lname);
        values.put(DatabaseWrapper.FUNTUBE_PHONE, phone);
        values.put(DatabaseWrapper.FUNTUBE_COUNTRY, country);
        values.put(DatabaseWrapper.FUNTUBE_GENDER, gender);
        values.put(DatabaseWrapper.FUNTUBE_VIDEOPATH, videopath);
        values.put(DatabaseWrapper.FUNTUBE_BDAY, bday);
        values.put(DatabaseWrapper.FUNTUBE_YEAR, year);
        values.put(DatabaseWrapper.FUNTUBE_INTEREST, interest);
        values.put(DatabaseWrapper.FUNTUBE_RELIGION, religion);
        values.put(DatabaseWrapper.FUNTUBE_ABOUT, about);
        values.put(DatabaseWrapper.FUNTUBE_QUOTE, qoute);
        values.put(DatabaseWrapper.FUNTUBE_JOB, job);



        long FuntubeId = database.insert(DatabaseWrapper.FUNTUBE, null, values);


        Cursor cursor = database.query(DatabaseWrapper.FUNTUBE, FUNTUBE_TABLE_COLUMNS, DatabaseWrapper.FUNTUBE_ID + " = " + FuntubeId, null, null, null, null);

        cursor.moveToFirst();


        FunTube newComment = parseFunTube(cursor);
        cursor.close();
        return newComment;
    }
    private FunTube parseFunTube(Cursor cursor) {
       FunTube ft = new FunTube();
        ft .setId((cursor.getInt(0)));
        ft .setUname(cursor.getString(1));
        ft .setEmail(cursor.getString(2));
        ft .setPassword(cursor.getString(3));
        ft .setFname(cursor.getString(4));
        ft .setLname(cursor.getString(5));
        ft .setPhone(cursor.getString(6));
        ft .setCountry(cursor.getString(7));
        ft .setGender(cursor.getString(8));
        ft .setVideopath(cursor.getString(9));
        ft .setBday(cursor.getString(10));
        ft .setYear(cursor.getString(11));
        ft.setInterest(cursor.getString(12));
        ft.setReligion(cursor.getString(13));
        ft .setAbout(cursor.getString(14));
        ft .setQuote(cursor.getString(15));
        ft .setJob(cursor.getString(16));


        return ft ;
    }
}

registration.java

public class registeration extends AppCompatActivity {

    private FunTubeOperations FunTubeDBOperations;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registeration);
        final AlertDialog ad = new AlertDialog.Builder(this).create();
        Button b1 = (Button) findViewById(R.id.register);
        FunTubeDBOperations = new FunTubeOperations(this);
        FunTubeDBOperations.open();

        try {
            b1.setOnClickListener(new View.OnClickListener() {


                @Override
                public void onClick(View arg0) {


                    EditText username = (EditText) findViewById(R.id.username);
                    EditText email = (EditText) findViewById(R.id.email);
                    EditText fname = (EditText) findViewById(R.id.fname);
                    EditText lname = (EditText) findViewById(R.id.lname);
                    EditText password = (EditText) findViewById(R.id.password);
                    EditText country = (EditText) findViewById(R.id.country);
                    EditText phone = (EditText) findViewById(R.id.phone);
                    EditText bday = (EditText) findViewById(R.id.bday);
                    EditText year = (EditText) findViewById(R.id.year);
                    EditText interest = (EditText) findViewById(R.id.interest);
                    EditText qoute = (EditText) findViewById(R.id.qoute);
                    EditText about = (EditText) findViewById(R.id.about);
                    EditText religion = (EditText) findViewById(R.id.religion);
                    EditText job = (EditText) findViewById(R.id.job);
                    RadioGroup rg = (RadioGroup) findViewById(R.id.fm);


                    String user = username.getText().toString();
                    String em = email.getText().toString();
                    String fn = fname.getText().toString();
                    String ln = lname.getText().toString();
                    String pass = password.getText().toString();
                    String co = country.getText().toString();
                    String ph = phone.getText().toString();
                    String bd = bday.getText().toString();
                    String ye = year.getText().toString();
                    String inter = interest.getText().toString();
                    String q = qoute.getText().toString();
                    String rel = religion.getText().toString();
                    String jo = job.getText().toString();
                    String ab = about.getText().toString();
                    String gen = ((RadioButton) findViewById(rg.getCheckedRadioButtonId())).getText().toString();


                    if (user != null) {
                        FunTube users = FunTubeDBOperations.addUsers(user, em, pass, ln, fn, ph, co, gen, null, bd, ye, inter, rel, ab, q, jo);
                        if (true) {
                            Toast.makeText(getApplicationContext(), (String) users.getUname() + " is added successfully!!", Toast.LENGTH_LONG).show();
                            Intent intent = new Intent(registeration.this, home.class);
                            startActivity(intent);
                            finish();
                        }
                    } else {
                        Toast.makeText(getApplicationContext(), "Add some Data...", Toast.LENGTH_LONG).show();
                    }
                }


            });


        } catch (Exception e) {
            ad.setTitle("Error!");
            ad.setMessage(e.toString());
        }


    }
    protected void onResume() {
        FunTubeDBOperations.open();
        super.onResume();
    }

    @Override
    protected void onPause() {
        FunTubeDBOperations.close();
        super.onPause();
    }
}
like image 439
tabia Avatar asked Jan 17 '26 20:01

tabia


2 Answers

Its simple you have define the column FUNTUBE_VIDEOPATH + " text not null," NOT NULL and you are inserting a null value from here

FunTube users = FunTubeDBOperations.addUsers(user, em, pass, ln, fn, ph, co, gen, null, bd, ye, inter, rel, ab, q, jo);

so your data not inserting to the database and returns -1 so your table dont have any data

now you are getting the data back from the db via this

Cursor cursor = database.query(DatabaseWrapper.FUNTUBE, FUNTUBE_TABLE_COLUMNS, DatabaseWrapper.FUNTUBE_ID + " = " + FuntubeId, null, null, null, null);

cursor.moveToFirst();

but you are getting not data because there is no data in db

Now pass value to videopath instead of the null value and then try

eg. replace this line

FunTube users = FunTubeDBOperations.addUsers(user, em, pass, ln, fn, ph, co, gen, null, bd, ye, inter, rel, ab, q, jo);

to

FunTube users = FunTubeDBOperations.addUsers(user, em, pass, ln, fn, ph, co, gen,"some value", bd, ye, inter, rel, ab, q, jo);

it will work

let me know in case of more issue

like image 163
Sandy Avatar answered Jan 20 '26 09:01

Sandy


// This should work
    private ArrayList<FunTube> parseFunTube(Cursor cursor) {
        ArrayList<Funtube> listofallmyFunTubes = new ArrayList<Funtube>(); 
       if (cursor != null) {
        // move cursor to first row
          if (cursor.moveToFirst()) {
            do {
            FunTube ft = new FunTube();
            ft .setId((cursor.getInt(0)));
            ft .setUname(cursor.getString(1));
            ft .setEmail(cursor.getString(2));
            ft .setPassword(cursor.getString(3));
            ft .setFname(cursor.getString(4));
            ft .setLname(cursor.getString(5));
            ft .setPhone(cursor.getString(6));
            ft .setCountry(cursor.getString(7));
            ft .setGender(cursor.getString(8));
            ft .setVideopath(cursor.getString(9));
            ft .setBday(cursor.getString(10));
            ft .setYear(cursor.getString(11));
            ft.setInterest(cursor.getString(12));
            ft.setReligion(cursor.getString(13));
            ft .setAbout(cursor.getString(14));
            ft .setQuote(cursor.getString(15));
            ft .setJob(cursor.getString(16));

            listofallmyFunTubes.add(ft); 
              // move to next row
            } while (cursor.moveToNext());

           }
           return listofallmyFuntubes;
    }
like image 30
Paul Avatar answered Jan 20 '26 10:01

Paul