简体   繁体   中英

Android app is crashing when value searched doesn't exist in database

When I search any value that does not exist in database, the app crashes instantly.

public String ifExistIn(String stationName) {
    String query = "SELECT stationName FROM review WHERE stationId" +
            "='" + stationName + "' OR " + " stationName " + "='" + stationName + "' LIMIT 1";
    try (SQLiteDatabase database = this.getWritableDatabase();
         Cursor c = database.rawQuery(query, null)) {
        c.moveToFirst();
        return c.getString(c.getColumnIndex("stationName"));
    }
}

The Activity adds available records to the ListView

public View.OnClickListener searchStation = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String searchString = searchText.getText().toString();
        //String useThis = dbHelper.ifExistIn(searchString);        
        if (list.contains(dbms.ifExistIn(searchString))) {
                AlertDialog.Builder builder = new AlertDialog.Builder(SearchAndReview.this);
                builder.setTitle("");
                builder.setMessage("RECORD ALREADY IN THE LIST ");
                builder.setNeutralButton("OK", null);
                builder.create().show();
            else {
                list.add(dbms.ifExistIn(searchString));
                listView.setAdapter(arrayAdapter);
            }
        } 
    }
};

Android studio log details Fatal exception

04-27 13:13:27.386  19845-19845/com.example.fahimghl.reviewapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.fahimghl.reviewapplication, PID: 19845
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
        at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
        at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
        at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
        at com.example.fahimghl.reviewapplication.DatabaseHelper.ifExistIn(DatabaseHelper.java:130)
        at com.example.fahimghl.reviewapplication.SearchAndReview$10.onClick(SearchAndReview.java:172)
        at android.view.View.performClick(View.java:4761)
        at android.view.View$PerformClick.run(View.java:19767)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5312)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

When I search any value that does not exist in database app crashes instantly.

Then probably you are querying an empty Cursor.

try (SQLiteDatabase database = this.getWritableDatabase();
        Cursor c = database.rawQuery(query, null)) {
        if (c != null && c.moveToFirst()) {
           return c.getString(c.getColumnIndex("stationName"));
        }
}
return null;

be aware that the try-with-resources requires at least api level 19

    public String ifExistIn(String stationName) {

String query = "SELECT stationName FROM review WHERE stationId" +
        "='" + stationName + "' OR " + " stationName " + "='" + stationName + "' LIMIT 1";
try (SQLiteDatabase database = this.getWritableDatabase();
     Cursor c = database.rawQuery(query, null))


 if (cursor.moveToFirst()) {
        do {
    String stationName = c.getString(c.getColumnIndex("stationName"));
  } while (cursor.moveToNext());
    }
    database.close();
    c.close();

return stationName;
}

Please always close database and cursor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM