java - 从 SQL 数据库中删除条目后出现空指针异常

标签 java android database android-studio

我正在为机器人开发一个应用程序,用户可以在其中定义机器人稍后从设备中获取的打洞组合。为了允许用户存储这些培训,我定义了一个类“Trainings”,其中包含培训的 ID、名称和冲压组合。该培训后来保存在数据库中,为此我编写了一个 DatabaseHandler 类。添加和显示数据工作正常,但每当我想使用以下方法删除条目时:

    public void deleteTraining(Training training) {
      SQLiteDatabase db = this.getWritableDatabase();

      db.delete(TABLE_TRAININGS, KEY_ID + " = ?",
      new String[] { String.valueOf(training.getID()) });
      db.close();
}

然后尝试再次填充我的 GridView(由 GridAdapter 类处理),我得到一个 Nullpointer 异常

java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.noeth.tobi.mcrobektrainingsplaner.Training._name' on a null object reference
at com.noeth.tobi.mcrobektrainingsplaner.GridAdapter.getView(GridAdapter.java:50)

GridAdapter 的 getView 方法:

public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        // if it's not recycled, initialize some attributes
        btn = new Button(context);
        btn.setLayoutParams(new GridView.LayoutParams(370, 350));
        btn.setPadding(2,100,2,100);
        btn.setOnClickListener(new CustomOnClickListener(position, context));
        btn.setOnLongClickListener(new CustomOnLongClickListener(position, context, btn));

    }
    else {
        btn = (Button) convertView;
    }

    btn.setText(db.getTraining(position)._name); //Here the programm throws a Nullpointer Exception AFTER deleting an entry from the database

    btn.setTextColor(Color.WHITE);
    btn.setBackgroundResource(R.drawable.button_border);
    btn.setTag("not_activated");
    btn.setId(position);

    return btn;
}

我认为它一定与删除训练的 ID 有关,因为循环只是遍历所有 ID,所以我写了一个方法 recalcIDs,它重新计算删除训练后出现的每个项目的 ID:
recalcIDs

public void recalcIDs(){
    int k = 1;
    int subtract = 1;
    int id;
    Training training;

    for(int i = deleted.get(0)+1; i < db.getTrainingCount(); i++){
        if(deleted.size() > 1){
            if(i < deleted.get(k)){
                training = db.getTraining(i);
                id = training.getID();
                training.setID(id-subtract);
            }
            else{
                k+=1;
                subtract+=1;
            }
        }
        else{
            training = db.getTraining(i);
            id = training.getID();
            training.setID(id-subtract);
        }


    }

} 

但这并不能解决问题。
当重新安装应用程序并从一个全新的数据库开始时,一切都会重新开始。
有人知道我做错了什么吗?

P.S.: 这是找不到名称的 getTraining 方法:

Training getTraining(int id) {
    SQLiteDatabase db = this.getReadableDatabase();
    Training training;
    Cursor cursor = db.query(TABLE_TRAININGS, new String[] { KEY_ID,
                    KEY_NAME, KEY_SK}, KEY_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null && cursor.moveToFirst()){

     training = new Training(Integer.parseInt(cursor.getString(0)),
            cursor.getString(1), cursor.getLong(2));
        cursor.close();
    }
    else{
        training = null;
        Toast.makeText(con,"Couldn't find any training sessions!", Toast.LENGTH_LONG).show();
    }

    // return training
    return training;
}

最佳答案

我假设您的 Training.setId 方法不调用数据库。 您不应该更改培训的 ID,因为它们由底层数据库管理。如果您只更改应用程序逻辑中的 ID,则两个数据集(应用程序和数据库)都会不同。 我建议在用户决定删除一个培训后从数据库中重新加载所有培训,然后调用 Gridview.notifyDatasetChanged

关于java - 从 SQL 数据库中删除条目后出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38932630/

相关文章:

android - 如何信任我的应用程序中的所有自签名证书

mysql - 用 VB 中的数据库表填充组合框

java - 收到错误消息 "Code Recommenders cannot download its model repository index"

android - 如何获取联系人创建时间

java - Android 特定的通过在谓词上匹配集合中的属性来查找对象

database - Grails 一对一查询选项

mysql - 将我的数据结构存储在数据库中的最佳方式是什么?

java - 凯撒之盒加密/解密程序

java - java中如何知道jar文件的目录?

java - Spring Boot 使用 Yaml 而不是属性文件