I am working on a robot application where the user can define the punch combinations that the robot will later select from the device. To allow the user to store these workouts, I defined the Workouts class, which contains the identifier, name, and combination of exercises. This training is later stored in the database for which I wrote the DatabaseHandler class. Adding and displaying data works fine, but whenever I want to delete a record using the method below:
public void deleteTraining(Training training) { SQLiteDatabase db = this.getWritableDatabase(); db.delete(TABLE_TRAININGS, KEY_ID + " = ?", new String[] { String.valueOf(training.getID()) }); db.close(); }
and then try filling my GridView again (handled by the GridAdapter class), I get a Nullpointer exception
java.lang.NullPointerException: attempt to read from the 'java.lang.String com.noeth.tobi.mcrobektrainingsplaner.Training._name' field in a null object reference
at com.noeth.tobi.mcrobektrainingsplaner.GridAdapter.getView (GridAdapter.java:50)
getView method for GridAdapter:
public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { // if it 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; }
I realized that it should have something to do with the id of the remote workout, because the loop just goes through all the identifiers, so I wrote a recalcID of a method that recalculates the identifier of each element following the remote workout:
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); } } }
However, this cannot be fixed. When you reinstall the application and starting with a completely new database, everything works again.
Does anyone know what I did wrong?
PS: Here's the getTraining method, where it cannot find the name:
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(); }