Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7558185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:18:27+00:00 2026-05-30T12:18:27+00:00

Here is the Mp_DB class and the method getMPName() and delete() method are posted.

  • 0

Here is the Mp_DB class and the method getMPName() and delete() method are posted. getMP_Name() method return string contains the name of the MP.

public class MP_DB extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "MP.db";
private static final String MP_TABLE_NAME = "MPData";

MP_DB (Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}// end of 

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(" CREATE TABLE " + MP_TABLE_NAME + " ( " +
            BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            " name TEXT, " +
            " lat REAL, " +
            " lng REAL, " +
            " date TEXT, " +
            " time TEXT " +
            ");" );
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}

public String getMP_Name(long id) {
    SQLiteDatabase db = this.getReadableDatabase();
    SQLiteCursor c = (SQLiteCursor) db.rawQuery("SELECT name FROM MPData WHERE "+
                                                BaseColumns._ID+" = "+
                                                Long.toString(id), null);
    c.moveToFirst();
    String r = c.getString(0);
    return r;       
}

I get the following error:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidbook.MP/com.androidbook.MP.MyLocations}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3691)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
at com.androidbook.MP.MP_DB.getMP_Name(MP_DB.java:44)
at com.androidbook.MP.MyLocations.get_MPNames(MyLocations.java:179)
at com.androidbook.MP.MyLocations.onCreate(MyLocations.java:59)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
... 11 more
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:18:28+00:00Added an answer on May 30, 2026 at 12:18 pm

    Your query doe snot return any results here:

    SQLiteCursor c = (SQLiteCursor) db.rawQuery("SELECT name FROM MPData WHERE "+
                                                BaseColumns._ID+" = "+
                                                Long.toString(id), null);
    c.moveToFirst();
    String r = c.getString(0);
    

    Thus the last line throws the error. Please make sure the query should return the expected value.

    Also I never query my database with raw query. Try doing it that way:

    String tableName = "MPData";
    String [] columns = { "name" };
    String where = BaseColumns._ID + "=?";
    String [] whereArgs = { Long.toString(id) };
    SQLiteCursor c = (SQLiteCursor) db.query(tableName, columns, where, whereArgs, null, null, null);
    

    Afterwords make the following check to see if the query returned any results:

    if (c.getCount() > 0) {
        c.moveToFirst();
        r = c.getString(0);
    } else {
        // the query returned nothing. Do appropriate action.
    }
    

    Also never forget to close your databases and cursors (or you will get errors). So the final version of the method:

    public String getMP_Name(long id) {
        SQLiteDatabase db = this.getReadableDatabase();
        String tableName = "MPData";
        String [] columns = { "name" };
        String where = BaseColumns._ID + "=?";
        String [] whereArgs = { Long.toString(id) };
        SQLiteCursor c = (SQLiteCursor) db.query(tableName, columns, where, whereArgs, null, null, null);
    
        String r = "";
        if (c.getCount() > 0) {
            c.moveToFirst();
            r = c.getString(0);
        } else {
            // the query returned nothing. Do appropriate action.
        }
        cursor.close();
        db.close();
        return r;       
    }
    

    PS: Mind my edits of your question. Formatting it that way makes it quite easier for users to read.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's going on here? printf.sh: #! /bin/sh NAME=George W. Bush printf Hello, %s\n $NAME
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here is a simplification of my database: Table: Property Fields: ID, Address Table: Quote
Here is my code, which takes two version identifiers in the form 1, 5,
Here's a coding problem for those that like this kind of thing. Let's see
Here is the scenario: I'm writing an app that will watch for any changes
Here's an interesting problem. On a recently installed Server 2008 64bit I opened IE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.