Search code examples
javaandroidsqliteandroid-cursor

android cursorindexoutofbounds index 0 requested


I am trying to return a row (one per table) from the table of whatever fRate is equal to (a currency symbol, USD, JPY, etc) and then convert the column from that row to a float. Whenever spinner2 is set to something aside the default selection (which is AUD, the first table in my db) I get the error.

    Button convert = (Button) rootView.findViewById(R.id.button1);
    convert.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            context = getActivity().getApplicationContext();
            DataBaseHelper dbHelper = new DataBaseHelper(context);
            try {
                dbHelper.createDataBase();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            dbHelper.openDataBase();
            db = dbHelper.getWritableDatabase();

            String lRate = spinner1.getSelectedItem().toString();
            String fRate = spinner2.getSelectedItem().toString();       
            Cursor c = db.rawQuery("SELECT * FROM "+fRate+" ", null);
            c.moveToNext();
            Float cfRate = c.getFloat(c.getColumnIndexOrThrow(lRate));
                c.close(); 



            answer.setText(String.valueOf((Float.parseFloat(amount.getText().toString()) * cfRate)));
            inputManager.hideSoftInputFromWindow(getView().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            db.close();
        }
    });

I keep getting the error at this line:

Float cfRate = c.getFloat(c.getColumnIndexOrThrow(lRate));

Solution

  • The tables other than AUD are empty, so you get an error telling you that no item could be found. You need to populate those tables with data if you want them to work.