Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当我插入数据库时出现NullPointerException(android)_Android_Database_Insert - Fatal编程技术网

当我插入数据库时出现NullPointerException(android)

当我插入数据库时出现NullPointerException(android),android,database,insert,Android,Database,Insert,我有一些数据要写入数据库(三个字符串); 我有一个私有类DBHelper扩展了SQLiteOpenHelper 我有dataBase=dbHelper.getWritableDatabase() 当我执行dataBase.insert()时,我有一个nullPointerException 我在Debugger中检查了数据的写入,它们不等于null DBHelper: private class DBHelper extends SQLiteOpenHelper { //for write an

我有一些数据要写入数据库(三个字符串); 我有一个私有类DBHelper扩展了SQLiteOpenHelper 我有dataBase=dbHelper.getWritableDatabase() 当我执行dataBase.insert()时,我有一个nullPointerException

我在Debugger中检查了数据的写入,它们不等于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) {

    }
}
我在数据库上写入数据的方法:

 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();
    }

}
“重要性=错误组=无组perskey=0R11434343”

您必须在单引号中加上Without group,如:
“Without group”
,否则SQLite会认为第二个单词是SQL查询的一部分。

是一个关键字,因此如果表的列名为this,则可能会出问题。 看


尝试将插入到数据库中的所有文本置于引号之间。另一种假设是,SQLite不太喜欢使用SQL关键字作为列名(如组)。。问题是它是一个保留关键字“SQLite上的组”。然后我在模拟器上安装了旧数据库。。thx为您提供帮助。我将所有组重命名为GROUPPS(用于测试);现在我又遇到了另一个问题(我在帖子中更改了LogCat)。但是我的查询有这个专栏…我解决了我的问题。。问题是它是一个保留关键字“SQLite上的组”。然后我在模拟器上安装了旧数据库。。thx为您提供帮助。检查>>>>静态最终字符串GROUPPS=“GROUPPS”之间的差异;表Persons没有名为groupp的列:>>>>请注意's'
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 (?,?,?)
group=Without group