0

I am using SQLite in Android eclipse, however it gives me a java.lang.nullpointerexception in the function createEntry. I tried using Questoid SQLite manager to view the database file and it does show up with the table created. Where's the bug?

public class Transact {

    public static final String KEY_ROWID = "_id";
    public static final String KEY_TAG = "saved_tag";

    private static final String DATABASE_NAME = "MyDatabaseName";
    private static final String DATABASE_TABLE = "tagsTable";
    private static final int DATABASE_VERSION = 1;

    private DbHelper ourHelper;
    private final Context ourContext;
    private SQLiteDatabase ourDatabase;

    private static class DbHelper extends SQLiteOpenHelper{

        public DbHelper(Context context){
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL(
                    "CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_TAG + " TEXT NOT NULL);"
                    );
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
            onCreate(db);

        }
    }

    public Transact(Context c ){
        ourContext = c;
    }

    public Transact open() throws SQLException{
        ourHelper = new    DbHelper(ourContext);
        ourHelper.getWritableDatabase();
        return this;
    }

    public void close(){
        ourHelper.close();
    }

    public long createEntry(String tagword) {
        // TODO Auto-generated method stub
        ContentValues cv = new ContentValues();
        cv.put(KEY_TAG, tagword);
        return ourDatabase.insert(DATABASE_TABLE, "", cv);
    }

    public String getData() {
        // TODO Auto-generated method stub
        String[] columns = new String[]{KEY_ROWID, KEY_TAG};
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
        String result = "";
        int iRow = c.getColumnIndex(KEY_ROWID);
        int iTag = c.getColumnIndex(KEY_TAG);
        for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
         result = result + c.getString(iRow) + " " + c.getString(iTag) + "\n";     
        }

        return null;
    }

}

Code for the add button from Main.java class:

case R.id.addDB:

    boolean doneAdd = true;
    try{
        String tag = textTag.getText().toString();
        Transact entry = new Transact(Main.this);
        entry.open();
        entry.createEntry(tag);
        entry.close();

    }catch(Exception e){
        String error = e.toString();
        Dialog d = new Dialog(this);
        d.setTitle("Error");
        TextView tv = new TextView(this);
        tv.setText(error);
        d.setContentView(tv);
        d.show();
        doneAdd = false;
    }finally{
        if(doneAdd){
            Dialog d = new Dialog(this);
            d.setTitle("Addition done");
            TextView tv = new TextView(this);
            tv.setText("Success");
            d.setContentView(tv);
            d.show();
        }
    }
break;
2
  • 4
    NullPointerException, on what line number? Commented Jun 4, 2013 at 5:09
  • The exception was occuring at return ourDatabase.insert(DATABASE_TABLE, "", cv); Solved now. Thanks. Commented Jun 4, 2013 at 5:41

2 Answers 2

5

You are using ourDatabase variable without initializing it. So not only insert you will get nullpointerexception everywhere where you are using ourDatabase variable

you can use something like following in constructor.

SQLiteDatabase db = context.openOrCreateDatabase(DATABASE_NAME,
                Context.MODE_PRIVATE, null);
Sign up to request clarification or add additional context in comments.

2 Comments

Isn't that what ourHelper.getWritableDatabase(); doing?
@user2450275 It should be ourDatabase = ourHelper.getWritableDatabase();
0
    used this code in sq light java file


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class SQLiteAdapter {
    Cursor cursor;
    public static final String MYDATABASE_NAME = "MY_DATABASE";
    public static final String MYDATABASE_TABLE = "MY_TABLE";
    public static final int MYDATABASE_VERSION = 2;
    public static final String KEY_ID = "_id";
    public static final String KEY_CONTENT1 = "Content1";
    public static final String KEY_CONTENT2 = "Content2";
    public static final String KEY_CONTENT3 = "Content3";
    public static final String KEY_CONTENT4 = "Content4";
    public static final String KEY_CONTENT5 = "Content5";
    public static final String KEY_CONTENT6 = "Content6";
    public static final String KEY_CONTENT7 = "Content7";
    public static final String KEY_CONTENT8 = "Content8";
    public static final String KEY_CONTENT9 = "Content9";
    public static final String KEY_CONTENT10 = "Content10";
    public static final String KEY_CONTENT11 = "Content11";
    public static final String KEY_CONTENT12 = "Content12";

// create table MY_DATABASE (ID integer primary key, Content text not null);
    private static final String SCRIPT_CREATE_DATABASE = "create table "
            + MYDATABASE_TABLE + " (" + KEY_ID
            + " integer primary key autoincrement, " + KEY_CONTENT1
            + " text not null, " + KEY_CONTENT2 + " text not null, "
            + KEY_CONTENT3 + " text not null, " + KEY_CONTENT4
            + " text not null, " + KEY_CONTENT5 + " text not null, "
            + KEY_CONTENT6 + " text not null, " + KEY_CONTENT7
            + " text not null, " + KEY_CONTENT8 + " text not null, "
            + KEY_CONTENT9 + " text not null, " + KEY_CONTENT10
            + " text not null, " + KEY_CONTENT11 + " blob not null, "
            + KEY_CONTENT12 + "  long not null);";


    private SQLiteHelper sqLiteHelper;
    private SQLiteDatabase sqLiteDatabase;

    private Context context;

    public SQLiteAdapter(Context c) {
        context = c;
    }

    public SQLiteAdapter openToRead() throws android.database.SQLException {
        sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null,
                MYDATABASE_VERSION);

        sqLiteDatabase = sqLiteHelper.getReadableDatabase();

        return this;
    }

    public SQLiteAdapter openToWrite() throws android.database.SQLException {
        sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null,
                MYDATABASE_VERSION);
        sqLiteDatabase = sqLiteHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        sqLiteHelper.close();
    }

public class SQLiteHelper extends SQLiteOpenHelper {

        public SQLiteHelper(Context context, String name,
                CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL("PRAGMA foreign_keys=ON");
            db.execSQL(SCRIPT_CREATE_DATABASE);

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub

        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.