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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:18:34+00:00 2026-06-10T19:18:34+00:00

All i want to do is set a string equal to one of the

  • 0

All i want to do is set a string equal to one of the entries in my database. The table i am accessing has 1 column (besides the ID column). I have been struggling with this for awhile so and even though similar questions have been asked, i am unable to make this situation work. I am comfortable with some other commands in SQLite but still a beginner. Here is what i have. I’m pretty sure once this method works my call from anther class will work fine.

note that both StringCategory_Table and COLUMN_CATEGORIES are defined as Strings.

my database was created with the following (it works fine for everything else except the below method):

db.execSQL("CREATE TABLE " + StringCategory_Table + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_CATEGORIES + " TEXT NOT NULL);");

and here is the method in question (created in a database class using SQLite open helper):

public String getCategory(int catId){
Cursor mycursor = myDatabase.rawQuery("SELECT " + COLUMN_CATEGORIES  + " FROM "+ StringCategory_Table +" WHERE " + _ID + " = " + catId, null);

//i no longer use this line:String strCatName = mycursor.toString();


String strCatName = mycursor.getString(mycursor.getColumnIndex(COLUMN_CATEGORIES));

        return strCatName;
    }

here is my logcat for the above code:

09-04 20:05:47.764: E/AndroidRuntime(9232): FATAL EXCEPTION: main
09-04 20:05:47.764: E/AndroidRuntime(9232): java.lang.RuntimeException: Unable to instantiate activity  ComponentInfo{com.mypackage.namespace/com.mypackage.namespace.MyCurrentRestaurants}: java.lang.NullPointerException
09-04 20:05:47.764: E/AndroidRuntime(9232):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
09-04 20:05:47.764: E/AndroidRuntime(9232):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-04 20:05:47.764: E/AndroidRuntime(9232):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-04 20:05:47.764: E/AndroidRuntime(9232):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-04 20:05:47.764: E/AndroidRuntime(9232):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 20:05:47.764: E/AndroidRuntime(9232):     at android.os.Looper.loop(Looper.java:137)
09-04 20:05:47.764: E/AndroidRuntime(9232):     at android.app.ActivityThread.main(ActivityThread.java:4424)
09-04 20:05:47.764: E/AndroidRuntime(9232):     at java.lang.reflect.Method.invokeNative(Native Method)
09-04 20:05:47.764: E/AndroidRuntime(9232):     at java.lang.reflect.Method.invoke(Method.java:511)
09-04 20:05:47.764: E/AndroidRuntime(9232):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-04 20:05:47.764: E/AndroidRuntime(9232):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)

Rather than Delete this question, I will explain what the problems were (as it will be annoying to dig through the comments):

  1. I can’t just assign a Class String to a SQLite entry as i was doing. Instead, i have to open the database before i can read from it: Since i need to open the database before i can read from it, it follows that i need to create an instance of the SQLite database class.
  2. Since i am using a cursor, i need to use the moveToFirst method before anything will work.

Thanks to everyone who has helped me today, and please let me know if these conclusions are incorrect as i don’t want to spread misconceptions to people as confused as i am.
Adam

  • 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-10T19:18:35+00:00Added an answer on June 10, 2026 at 7:18 pm

    Try this,

    public String getCategory(int catId){
            Cursor mycursor = myDatabase.rawQuery("SELECT " + COLUMN_CATEGORIES  + " FROM "+ StringCategory_Table +" WHERE " + _ID + " = " + catId, null);
          if(mycursor.moveToFirst()){ //Edited based on suggestion from SAM
              String strCatName = mycursor.getString(mycursor.getColumnIndex(COLUMN_CATEGORIES));
                      return strCatName;
            } else {
            return null;
             }
    
    
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base class for all my textboxes and I want to set
I want to get all values of a set interface in one go as
I have a recursive function that parses a trie string database replacing with all
I want all my inno set up pages to look like below :
I want to set the Expires header for all image/* and text/css . I'm
I want to use all the photos in a Flickr set and in order
I want to retrieve a set of results, which consist of all results produced
I've connected to database, using data set .xsr now I wan't to extract all
I have a table with about 1000 records and 2000 columns. What I want
I have a problem which has been torturing for many hours. I need to

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.