1

I have used LogCat to determine the problem line.

The SQL String I am trying to execute in onCreate is..

CREATE TABLE Routines(_id integer primary key autoincrement, json TEXT);

When it tries to execute this, the problem occurs.

This may well be whats causing the NullPointerException. If you can't see anything wrong with that, please read on for a bit more background.

This database has not been created yet, I keep getting a NullpointerException, I have compared this to previous SQLite code and the problem is proving evasive.

However, I mention this as it still has to go through the onCreate method. I create a new DatabaseOpenHelper (extending SQliteOpenHelper) in my main code and call the helper's open() method as seen below.

public void open() throws SQLException
{
    ssDatabase = databaseOpenHelper.getWritableDatabase();
}

If I am not mistaken, as my database has not been created (I made sure of that through uninstalling it prior). The onCreate SQLiteHelper is invoked when that open() method is called.

This is the code where I call that open method.

    try
    {                   
        dbConnector = new DatabaseConnector (this);
        debug = "2 ";

        // This on first start will invoke the database onCreate method - throws SQLException
        dbConnector.open();  /* PROBLEM LINE */
        debug = "3 ";
    }
    catch (Exception e)
    {
        textView1.setText(debug + e.toString());
    }

And this is the code containing my DatabaseOpenHelper and onCreate method

private class DatabaseOpenHelper extends SQLiteOpenHelper
{
    public DatabaseOpenHelper(Context context, String name, CursorFactory factory, int version)
    {
        super(context, name, factory, version); 

        Log.i(TAG, "Constructor");
    }

    // On initial creation of database
    @Override
    public void onCreate(SQLiteDatabase db)
    {
        Log.i(TAG, "Before SQL command");

        String sqlCreateCommand = "CREATE TABLE Routines"
                + "(_id integer primary key autoincrement, "
                + "json TEXT);";

        Log.i(TAG, sqlCreateCommand); 

        // I believe this to be the PROBLEM LINE
        ssDatabase.execSQL(sqlCreateCommand); 

        Log.i(TAG, "Jab done");
    }

    // On upgrade - currently do nothing
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {}      
}   

Thanks for the help!

mgibson
  • 6,103
  • 4
  • 34
  • 49
  • The complete stack trace would be useful as well :) – span Dec 19 '12 at 13:14
  • Did you try debugging your code to find it? – Adam Arold Dec 19 '12 at 13:19
  • Hmm, if by that you mean errors that are in the 'problems' tab, there are none from this path at all. The only exception as in the one I am referring to I displayed on a TextView.. maybe a bit of a noob thing to do :P And there on my LogCat either with the exception being caught aside from my own tags. – mgibson Dec 19 '12 at 13:26
  • Yeah I have been trying for a while LogCatting and using the Eclipse debugger. But with the Eclipse debugger, when I come to the execSQL line, it gives this Class File Editor - Source not found message and I don't really know much about it or how to interpret it – mgibson Dec 19 '12 at 13:30

1 Answers1

4

seems like i dont have the prvilege to just post a comment. One question: ssDatabase.execSQL(sqlCreateCommand); --> shouldn't this be db.execSQL(sqlCreateCommand); since the parameter is SQLiteDatabase db. Secondly you use _id. is this index not created by default ? Nevermind if this has nothing to do with it. This were just my thoghts about it. :)

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
JustCoding
  • 399
  • 1
  • 5
  • 18
  • Ahh what a fool I am! Haha, I guess thats what hours of coding does to you, well to me! Thanks! And as far as I know, it is not default, "_id" is recommended and allows special functionality with ContentProviders but it still has to be set. That is what I have gathered from a bunch of tutorials so far. – mgibson Dec 19 '12 at 13:40