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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:02:34+00:00 2026-05-16T07:02:34+00:00

I’m new to the Android framework and I can’t get past first base with

  • 0

I’m new to the Android framework and I can’t get past first base with SQLite.

I’m trying to build a very simple application that has a EditText search box, which when a key is pressed performs a Like %word% search on a SQLite database for for the text entered in the EditText box, and displays the results in a ListView.

N.B. the below code included debugging and commented code, sorry I haven’t got round to cleaning it up yet.

The Activate is

public class sustainable_fish extends Activity {

private String keycodeToAscii(int arg2){

    String retvar = "";

    switch(arg2){
    case KeyEvent.KEYCODE_A: retvar = "a";break;
    case KeyEvent.KEYCODE_B: retvar = "b";break;

snipped, but you get the idea.

    case KeyEvent.KEYCODE_RIGHT_BRACKET: retvar = ")";break;
    default: retvar = ""; break;
    };
    return retvar;

}



 Context that = getApplicationContext();
 fishDB dh = new fishDB(getApplicationContext());


private OnKeyListener search = new OnKeyListener() {

    public boolean onKey(View arg0, int arg1, KeyEvent arg2) {


        @SuppressWarnings("unused")
        Cursor cur = null;
        String searchData;
        EditText myEditText = (EditText) findViewById(R.id.fish_search);
        CharSequence edit_text_value = myEditText.getText();
        searchData = edit_text_value.toString();
        searchData = searchData + keycodeToAscii(arg2.getKeyCode() );
        Log.d("onKey", "searchData = "+searchData);
        /*
        Commented out because as the database doesn't work, so theirs no point just yet searching.

        cur = fish.search(searchData);
        if (cur != null)
        {
            String[] displayFields = new String[] {cur.getString(2),
                    cur.getString(1)};

            int[] displayViews = new int[] { R.id.fish_rank,
                    R.id.fish_name};

            SimpleCursorAdapter adapter = new SimpleCursorAdapter(that, 
                    R.layout.text_list, cur, 
                    displayFields, displayViews);

            ListView myList=(ListView)findViewById(android.R.id.list);

            myList.setAdapter(adapter);
        } 
        */

        return false;
    }

};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

   setContentView(R.layout.main);
   EditText searchBar = (EditText) findViewById(R.id.fish_search);
   searchBar.setOnKeyListener(search); 

}

}

The SQLite database is handled by fishDB

  public class fishDB {

 public static final String DATABASE_NAME = "fish.db3";
 public static final int DATABASE_VERSION = 1;
 private SQLiteDatabase database;
 private Context myContext;
 private static boolean booFirstrun = false;

 fishDB(Context inContext){
     myContext = inContext;
     OpenHelper helper = new OpenHelper(myContext);
     Log.w("fishDB", "Called OpenHelper");

             // Here be the problem...

     database = helper.getWritableDatabase();

 }

 public boolean firstRun(){
    return booFirstrun;
 }


   private static class OpenHelper extends SQLiteOpenHelper {

       private static final String CREATE_TABLE_FEEDS = "create table feeds (feed_id integer primary key autoincrement, "
           + "title text not null, url text not null);";

          OpenHelper(Context context) {
             super(context, DATABASE_NAME, null, DATABASE_VERSION);
             Log.w("OpenHelper", "Called superconstructor");
            }

          @Override
          public void onCreate(SQLiteDatabase db) {
              try {
              Log.w("OpenHelper", "in onCreate");
              booFirstrun = true;
              Log.w("OpenHelper", "set booFirstrun");
              db.beginTransaction();
              Log.w("OpenHelper", "db.beginTransaction");
              try {
                  // Create tables and test data
                  db.execSQL(CREATE_TABLE_FEEDS);
                  Log.w("OpenHelper", "execSQL");
                  db.setTransactionSuccessful();
                  Log.w("OpenHelper", "setTransactionSuccessful");
              } catch (SQLException e) {
                  Log.e("Error creating tables and debug data", e.toString());
                  throw e;
              } finally {
                  db.endTransaction();
              }
              } catch (Exception e) {
                  Log.e("OpenHelper", e.toString() );
              } finally {
              Log.w("OpenHelper", "out of onCreate");
              }
          }

          @Override
          public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
             Log.w("OpenHelper", "Upgrading database, this will drop tables and recreate.");
             onCreate(db);
          }
       } 
 }

Now every time “database = helper.getWritableDatabase();” is called the app crashes with a NullPointerException. I’ve managed to trace the error to the onCreate method of OpenHelper and the “db.beginTransaction();” line.

What am I missing?

  • 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-16T07:02:35+00:00Added an answer on May 16, 2026 at 7:02 am

    Actually you do not need to start transaction at onCreate method of the helper. Try removing everything from the method except db.execSQL(CREATE_TABLE_FEEDS);

    Update: I was able to repeat the problem.

    Move fishDb field initialization to the onCreate() method of activity after that error disappears. It looks like it is not good idea to start db initialization before the onCreate is invoked.

    So instead of

    fishDB fishDb = new fishDB(getApplicationContext());
    

    do

    fishDB fishDb;
    
    onCreate(){
      super.onCreate();
      fishDb = new fishDB(getApplicationContext());
      .... //init listener, query db etc..
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.