Roger
Roger

Reputation: 6527

SQLite give a NullPointerException

In my main code I have:

String TableLayout = "TAB01";
dbWorker db = new dbWorker();
db.createTable(TableLayout);

Which is supposed to create a dbWorker class object and make it create a table called TAB01.

package Constructor.rob.com;

import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.Toast;

public class dbWorker extends Activity {

    SQLiteDatabase mydb;
    private static String DBNAME = "TotalKeyboard.db";  // THIS IS THE SQLITE DATABASE FILE NAME.

    public void createTable( String TableName ){
        try{
        mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
        mydb.execSQL("CREATE TABLE IF NOT EXISTS "+ TableName +" (ID INTEGER PRIMARY KEY, NUMBER_OF_ROWS INTEGER, STYLE TEXT, BG1 INTEGER, BG1 INTEGER, FLAG TEXT);");
        mydb.close();
        }catch(Exception e){  Log.e("Error Creating!", e.toString() );  } }
...

Yet it catches me an "ERROR/Error Creating!(14823): java.lang.NullPointerException".

Any ideas, what might be wrong?

Thanks!

Upvotes: 0

Views: 538

Answers (2)

port443
port443

Reputation: 581

From what we see here, you can only get NullReferenceException if the TableName parameter passed is null.

Upvotes: 0

Eric Levine
Eric Levine

Reputation: 13564

You need to do things a little differently regarding the database. You should extend SqLiteOpenHelper, create an instance of that class, and call getReadableDatabase or getWriteableDatabase. Details are here: http://developer.android.com/guide/topics/data/data-storage.html#db

Upvotes: 2

Related Questions