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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:43:22+00:00 2026-06-14T22:43:22+00:00

I have a database table which has a column with the name notes. This

  • 0

I have a database table which has a column with the name “notes”.
This column’s data get only “yes” or “no” depending if the specific row is my favorite or not (add to favorites).

I am trying to update this column’s data with the following code:

public void updateFavorite(long id) 
   {
       String txt;
       System.out.println("first");
       Cursor c = database.query("recipes", null, "_id=" + id, null, null, null, null);
       c.moveToFirst();
       System.out.println("second");
       int notesIndex = c.getColumnIndex("notes");
       System.out.println("third");
       String fav=c.getString(notesIndex);
       System.out.println("fourth");

       System.out.println(fav);

       if (fav=="yes") {txt="no";}
       else {txt="yes";}

       ContentValues editEsoda = new ContentValues();

       editEsoda.put("notes", txt);

       open(); // open the database
       database.update("recipes", editEsoda, "_id=" + id, null);
       close(); // close the database
       c.close();
   } 

I am getting an error when trying to retrieve the Cursor c:

Cursor c = database.query("recipes", null, "_id=" + id, null, null, null, null);

I want to read the string at this row and column. If it is “yes” then turn it to “no”. Otherwise if it is “no” then turn it to “yes”.

Why am i getting an error there?
Thank you in advance

This is my LogCat:

11-26 19:00:25.404: D/dalvikvm(30378): GC_FOR_MALLOC freed 4678 objects / 265896 bytes in 79ms
11-26 19:00:25.444: I/System.out(30378): first
11-26 19:00:25.444: D/AndroidRuntime(30378): Shutting down VM
11-26 19:00:25.444: W/dalvikvm(30378): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-26 19:00:25.454: E/AndroidRuntime(30378): FATAL EXCEPTION: main
11-26 19:00:25.454: E/AndroidRuntime(30378): java.lang.IllegalStateException: Could not execute method of the activity
11-26 19:00:25.454: E/AndroidRuntime(30378):    at android.view.View$1.onClick(View.java:2072)
11-26 19:00:25.454: E/AndroidRuntime(30378):    at android.view.View.performClick(View.java:2408)
11-26 19:00:25.454: E/AndroidRuntime(30378):    at android.view.View$PerformClick.run(View.java:8816)
11-26 19:00:25.454: E/AndroidRuntime(30378):    at android.os.Handler.handleCallback(Handler.java:587)
11-26 19:00:25.454: E/AndroidRuntime(30378):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-26 19:00:25.454: E/AndroidRuntime(30378):    at android.os.Looper.loop(Looper.java:123)
11-26 19:00:25.454: E/AndroidRuntime(30378):    at android.app.ActivityThread.main(ActivityThread.java:4627)
11-26 19:00:25.454: E/AndroidRuntime(30378):    at java.lang.reflect.Method.invokeNative(Native Method)
11-26 19:00:25.454: E/AndroidRuntime(30378):    at java.lang.reflect.Method.invoke(Method.java:521)
11-26 19:00:25.454: E/AndroidRuntime(30378):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-26 19:00:25.454: E/AndroidRuntime(30378):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-26 19:00:25.454: E/AndroidRuntime(30378):    at dalvik.system.NativeStart.main(Native Method)
11-26 19:00:25.454: E/AndroidRuntime(30378): Caused by: java.lang.reflect.InvocationTargetException
11-26 19:00:25.454: E/AndroidRuntime(30378):    at development.nk.cretanrecipes.ViewRecipes.favorite(ViewRecipes.java:235)
11-26 19:00:25.454: E/AndroidRuntime(30378):    at java.lang.reflect.Method.invokeNative(Native Method)
11-26 19:00:25.454: E/AndroidRuntime(30378):    at java.lang.reflect.Method.invoke(Method.java:521)
11-26 19:00:25.454: E/AndroidRuntime(30378):    at android.view.View$1.onClick(View.java:2067)
11-26 19:00:25.454: E/AndroidRuntime(30378):    ... 11 more
11-26 19:00:25.454: E/AndroidRuntime(30378): Caused by: java.lang.NullPointerException
11-26 19:00:25.454: E/AndroidRuntime(30378):    at development.nk.cretanrecipes.DatabaseConnector.updateFavorite(DatabaseConnector.java:93)
11-26 19:00:25.454: E/AndroidRuntime(30378):    ... 15 more
11-26 19:00:29.094: I/Process(30378): Sending signal. PID: 30378 SIG: 9
  • 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-14T22:43:24+00:00Added an answer on June 14, 2026 at 10:43 pm

    I am getting an error when trying to retrieve the Cursor c
    Cursor c = database.query("recipes", null, "_id=" + id, null, null, null, null);

    That means the database is null, you need to use database = helper.getWritableDatabase() before this line and replace helper with your SQLiteOpenHelper variable. I’m guessing this is what open() does.

    Along with what the others have said lets make this method a little faster:

    public void updateFavorite(long id) 
    {
        open(); // open the database here!
        ContentValues editEsoda = new ContentValues();
    
        // Only fetch the columns that you are going to work with
        Cursor c = database.query("recipes", new String[] {"notes"}, "_id=" + id, null, null, null, null);
    
        // If a row exists with the given id
        if(c.moveToFirst()) {
            // Toggle the value of "notes", but this should be a boolean
            if(c.getString(0).equals("yes")
                editEsoda.put("notes", "no");
            else
                editEsoda.put("notes", "yes");
    
            c.close();
            database.update("recipes", editEsoda, "_id=" + id, null);
        }
        else {
            // No data has this id
        }
        close(); // close the database
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a Oracle 9i database and OrderDetails table which has a column to
I have a table in SQLite database of Android which has a column RANK
Ok... I have a database table called employees.. This has columns called ID, Name,
I have a MySQL database and a table which has a names column as
I have a column 'name' in Database which has values as 'john,smith'. I'm passing
I have a database table named tree1, which has a table names student_details.This table
I have a database table which contains an ID column and a Name column.
I have a database table A which stores records, A has a primary key
I have a table which has essentially boolean values in a legacy database. The
I have a database which has a table recording when a vehicle was last

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.