Reputation: 113
I am getting the following error:
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0.
From this code:
public Lirik getLirik(String id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_MUPUH,
new String[] { KEY_ID, JUDUL, KEY_MUPUH },
KEY_ID + "=?",
new String[] {
String.valueOf(id)
},
null,
null,
null);
if (cursor != null && cursor.moveToFirst())
cursor.moveToFirst();
Lirik lirik = new Lirik(cursor.getString(1),
cursor.getString(2));
db.close();
return lirik;
}
and this class ViewMupuh
:
error on : Lirik lirik = db.getLirik(position);
public class ViewMupuh extends Activity implements OnClickListener {
private static String position = null;
private TextView textJudul, textLirik;
private Button bUpdate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_mupuh);
Intent intent = getIntent();
position = intent.getStringExtra("position");
if (position != null) {
Log.d("value of position", position);
}
DBHelper db = new DBHelper(this);
Lirik lirik = db.getLirik(position);
textJudul = (TextView) findViewById(R.id.judul_details);
textJudul.setText(lirik.getJudul());
textLirik = (TextView) findViewById(R.id.lirikdetails);
textLirik.setText(lirik.getLirik());
bUpdate = (Button) findViewById(R.id.bupdatedetails);
bUpdate.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), EditLirik.class);
intent.putExtra("position value", position);
startActivity(intent);
}
}
Upvotes: 4
Views: 4370
Reputation: 18242
You have several problems in your code. First of all, you are calling cursor.moveToFirst()
twice innecesarily:
if (cursor != null && cursor.moveToFirst()) // <-- First call
cursor.moveToFirst(); // <-- Second call
Also, you are trying to get data from the cursor even if the cursor is null or it has retrieved no data:
if (cursor != null && cursor.moveToFirst())
cursor.moveToFirst();
Lirik lirik = new Lirik(cursor.getString(1),
cursor.getString(2)); // <-- cursor can be null here
Anyway, you are leaving resources open. You never close cursor
and if there is any problem, db
is not closed either. I suggest to rewrite your getLirik()
function this way:
public Lirik getLirik(final String id) {
Lirik lirik = null;
SQLiteDatabase db = null;
Cursor cursor = null;
try {
db = this.getReadableDatabase();
cursor = db.query(TABLE_MUPUH, new String[] { KEY_ID, JUDUL,
KEY_MUPUH }, KEY_ID + "=?", new String[] { id }, null,
null, null);
if (cursor != null && cursor.moveToFirst()) {
lirik = new Lirik(cursor.getString(1), cursor.getString(2));
}
} catch (final Exception e) {
// Do something with the Exception (Log, raise, ...)
} finally {
cursor.close();
db.close();
}
return lirik;
}
If you do so, take into account that you can get null when calling getLirik()
, so, in your ViewMupuh.onCreate()
you should check for it to avoid further errors:
textJudul = (TextView) findViewById(R.id.judul_details);
textLirik = (TextView) findViewById(R.id.lirikdetails);
DBHelper db = new DBHelper(this);
Lirik lirik = db.getLirik(position);
if (lirik != null) {
textJudul.setText(lirik.getJudul());
textLirik.setText(lirik.getLirik());
}
Also take into account that all the hints point to that there is no record in your database that matches the id you are querying for (maybe your database is empty?)
Upvotes: 3
Reputation: 3134
I think there are more than one problem here. The first problem is that the cursor does not have any row in it. The second problem is the following.
if (cursor != null && cursor.moveToFirst())
cursor.moveToFirst();
Lirik lirik = new Lirik(cursor.getString(1),
cursor.getString(2));
db.close();
return lirik;
there is no curly braces around the if statement. As a result, in the case where the cursor has no row, it is still executing Lirik lirik = new Lirik(cursor.getString(1),
cursor.getString(2));
Please make sure your database has the information you need.
Upvotes: 0