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

  • Home
  • SEARCH
  • 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 8753741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:29:01+00:00 2026-06-13T13:29:01+00:00

I make application and compiler show error. I don’t know what happened. I want

  • 0

I make application and compiler show error. I don’t know what happened. I want to retriev and display random record from database after click.
I used tutorial from Android SQLite Database Tutorial and from stackoverflov
Error:

    10-30 19:19:59.533: E/AndroidRuntime(359): FATAL EXCEPTION: main
10-30 19:19:59.533: E/AndroidRuntime(359): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
10-30 19:19:59.533: E/AndroidRuntime(359):  at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)

My code is:

public class PrzyslowiaArabskie  {

    private static final String TABLE_NAME = "tabela_przyslowia";
    private static final String DATABASE_NAME = "przyslowiadb";
    private static final int DATABASE_VERSION = 1;
    public static final String KEY_ID = "id";
    public static final String KEY_PRZYSLOWIE = "przyslowie";

    private DbHelper myHelper;
    private final Context myContext;
    private SQLiteDatabase myDatabase;




    private static class DbHelper extends SQLiteOpenHelper {

        public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
             db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + 
                       KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                     KEY_PRZYSLOWIE + " TEXT NOT NULL);" );


        } 

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            onCreate(db);
        }

    }
    public PrzyslowiaArabskie(Context c) {
        myContext = c;
    }
    public PrzyslowiaArabskie open() {
        myHelper = new DbHelper(myContext);
        myDatabase = myHelper.getWritableDatabase();
        return this;
    }
    public void close() {
        myHelper.close();
    }


    public String getData() {
        // TODO Auto-generated method stub
        String[] columns = new String[] { KEY_ID, KEY_PRZYSLOWIE };
        Cursor c = myDatabase.query(TABLE_NAME, columns, KEY_ID + "=?", new String[] {String.valueOf(KEY_ID + 1)}, null, null, null);
         if (c != null) {
             c.moveToFirst(); 
             String data = c.getString(1);
             return data;
         }  
        return null;
    }


}

What am I doing wrong?

I solved with my problem. Instead getting random record from database, I make 3 buttons (next record, previous record and show record). I use this instructions Getting the next record into view from database
Thanks for your help.

  • 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-06-13T13:29:02+00:00Added an answer on June 13, 2026 at 1:29 pm

    You are attempting to index into a column on a row of a cursor that does not exist.

    if (c != null) {
         c.moveToFirst(); // This needs to check for validity
         String data = c.getString(1); // Before indexing into a non-existent record
         return data;
    }
    

    Instead…

    if(c.moveToFirst()){
      String data = c.getString(1);
      return data;
    }
    

    Of course if you intend to iterate over multiple rows you might want to consider a while-loop and using while(c.moveToNext()) ...

    Edit: extended answer to address comment.

    To use a while loop to iterate over all records available through the cursor and utilize the data in consumer code it is likely you will have to change the method signature to return a collection of sorts instead of a single string.

    public ArrayList<String> getData() {
        ArrayList<String> data = new ArrayList<String>();
        String[] columns = new String[] { KEY_ID, KEY_PRZYSLOWIE };
        Cursor c = myDatabase.query(TABLE_NAME, columns, KEY_ID + "=?", new String[] {String.valueOf(KEY_ID + 1)}, null, null, null);
        while(null != c && c.moveToNext()) {
          data.add(c.getString(1));
        }
        return data;
    }
    

    The example code provided should get you the result you want but if you need more explanation on dealing with collections of objects and while loops I would suggest that you consult the Java documentation for some information or more abstract programming information in regard to dealing with data structures and control flow.

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

Sidebar

Related Questions

I want to make an application that would display a message when a user
I am in the process of porting my application from MinGW/GCC/make to cl compiler/nmake
I make an Application in java and draw rectangle. When I search from combobox
I want to make my application similar to mail.app in vertical orientation. It should
I want to make an Application that uses RxTx version 2.2pre2 to work with
I want to make an application witch involves logging into google. It is basically
I want to make an application in C or C++ which have to monitor
How do you make your application aware that a database record was changed or
I want to make my application compatible with as2, so I think what I
I have a Spring web application and I want to make unittests for my

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.