Vetalll
Vetalll

Reputation: 3700

NullPointerException when i do insert into database (android)

i have some data to write in data base (three strings); i have a private class DBHelper extends SQLiteOpenHelper and i have dataBase = dbHelper.getWritableDatabase(); When i do dataBase.insert() i have a nullPointerException.

i checked my data in debuger for write and they don't equal to null;

DBHelper:

private class DBHelper extends SQLiteOpenHelper { //for write and read data from DB

static final String GROUPPS = "groupps";
static final String IMPORTANCE = "importance";
static final String NAME_OF_TABLE = "Person";
static final String PERSON_KEY = "perskey";

    private static final String MY_QUERY = "CREATE TABLE " + NAME_OF_TABLE + " ( " +
            PERSON_KEY + " TEXT, " +
            GROUPPPS + " TEXT, " +
            IMPORTANCE + " TEXT);";

    public DBHelper(Context context) {
        // constructor of superclass
        super(context, "MyDBB", null, 2);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d("", "--- onCreate database ---");
        db.execSQL(MY_QUERY);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

my method for write data on the dataBase:

 public void writeDataToDB(ArrayList<Person> arrayList) {

    contentValues = new ContentValues();
    dbHelper = new DBHelper(activity.getApplicationContext());
    dataBase = dbHelper.getWritableDatabase();


    for (int i = 0; i < arrayList.size(); i++) {

        contentValues.put(PERSON_KEY, arrayList.get(i).getKey());
        contentValues.put(GROUPPPS, arrayList.get(i).getGroup());
        contentValues.put(IMPORTANCE, String.valueOf(arrayList.get(i).isImportant()));

        Log.e("writeDataToDB", "Start inserting " + contentValues.toString());
        long rowID = dataBase.insert(NAME_OF_TABLE, null, contentValues);
        Log.e("writeDataToDB - ", "Value inserted, rowID = " + rowID);
        contentValues.clear();
    }

}

And in my method i have exception at line : long rowID = dataBase.insert(NAME_OF_TABLE, null, contentValues);
and rowID = -1 it means - somthing wrong;

and my LogCat:

Error inserting groupp=Withoutgroup importance=false perskey=0r2-2B43414D272B4D16
    android.database.sqlite.SQLiteException: table Persons has no column named groupp: , while compiling: INSERT INTO Persons(groupp,importance,perskey) VALUES (?,?,?)

Upvotes: 0

Views: 486

Answers (2)

Its not blank
Its not blank

Reputation: 3095

Group is a keyword so if your table has this as column name then that may be problem. see

group=Without group

Upvotes: 1

jcxavier
jcxavier

Reputation: 2232

"importance=false group=Without group perskey=0r114343434343434343434343434343434343434343434343"

You have to put Without group inside single quotes, like this: 'Without group', or else SQLite will think the second word is part of the SQL query.

Upvotes: 1

Related Questions