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 7771647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:42:13+00:00 2026-06-01T16:42:13+00:00

My program is suppose to check the options table if it already set(inserted) an

  • 0

My program is suppose to check the options table if it already set(inserted) an option(e.g. volume, vibration). Basically, if an option is not set, the first column at the table is 0. If the first row is empty, then switch to default option, else, switch to the option set. I surround my option checker with a try catch an receive this error:

CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0.

I’m still newb to this sql stuff so I’m not sure if my codes are right to my needs.

This is my option checker at the OnCreate():

options = new DBFunctions(this);

    try{
    String row = "0".toString();
    long l = Long.parseLong(row);
    options.open();
    String retId = options.getId(l);
    int id = Integer.parseInt(retId);
    options.close();
    //codes for setting the options if/if no data in option table exists will be put here
    }catch(Exception e){
    String error = e.toString();
    tv.setText(error);
}       

This is my getId() in my DBFunctions. java:

public String getId(long l) {

    String[] columns = new String[]{"id", "volume", "vibrate", "theme"};
    Cursor c = db.query(table, columns, "id=" + l, null, null, null, null);
    if(c !=null){
        c.moveToFirst();
        String id = c.getString(1);
        return id;
    }
    return null;
}

`

  • 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-01T16:42:14+00:00Added an answer on June 1, 2026 at 4:42 pm

    You’re trying to get data from a Cursor that is empty(0 rows) and this throws that exception. I saw your other questions and I’ve seen that your id column in the options table is set as INTEGER PRIMARY KEY AUTOINCREMENT. AUTOINCREMENT means that this column will increment its value each time a row is inserted in the database and I think it is also starting at a value above 0(not sure). This means that the query you make in the getId method will always return an empty Cursor(you query for the rows with id 0 but there are none in the database).

    I didn’t understand the first part of your question the one with inserted/not inserted in the database and switching to defaults so I can say how to do what you want.

    Here is some sample code with SharedPreferences:

    // When you want to check the status of the options
    SharedPreferences prefs = PreferenceManager
                    .getDefaultSharedPreferences(this); //in an activity
        // volumeStatus, vibrateStatus and themeStatus represent the current status of the options
    boolean volumeStatus = prefs.getBoolean("volume_key", false); // if volumeStatus is false it is the first app run or the user put the volume to off
    boolean vibrateStatus = prefs.getBoolean("vibrate_key", false); // the same as for volumestatus
    String themeStatus = prefs.getString("theme_key", null); // if themeStatus is null it is the first run of the app or the user didn't choose a theme
    
        // when the user modifies one of the preferences use this
            SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean("volume_key", true); // the use sets the volume to on like in the database
                editor.commit();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My program is suppose to count the occurrence of each character in a file
I have a requirement such that my program is suppose to run for a
So I'm writing a program that is suppose to run forever (client server stuff)
Suppose I write a program using immutable data structures in Java. Even though it
Say suppose I am running a java program through command line. And this program
Suppose I have a program that initializes a global variable for use by threads,
Suppose public class Program { public static void Main(string[] args) { Program p =
Suppose I've got the following program: namespace ReflectionTest { public class Example { private
Suppose that I have a Java program within an IDE (Eclipse in this case).
Suppose you need to run a program on the world’s fastest supercomputer which will

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.