我有一个用户答案的列表视图(考试结果),这个listView从数据库中获取考试答案,我也想删除这个列表视图,在活动中我做了一个按钮(不在列表视图中)女巫删除了列表视图但是当我点击按钮给我这个错误时我知道发生了这个错误,因为我没有设置考试年份和问题编号,但我不知道如何在我的按钮onClick in列表视图中执行此操作:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String
com.example.eric.myapp6.model.User_answer.getKonkoor_num()' on a null object reference
at com.example.eric.myapp6.database.DataBase.deleteUser(DataBase.java:140)
at com.example.eric.myapp6.exam_resault.pasokh_azmoon$1.onClick(pasokh_azmoon.java:50)
用户答案是我用于获取设置方法的类。
这是我的数据库类:
private static final String TABLE_FAVOURITE_QUESTIONS = "favourite_questions";
private static final String TABLE_USER_EXAM = "User_exam";
private static final String TABLE_EXAM_FINAL_PERCENT = "Exam_final_percent";
private static final String Tozih = "tozih";
private static final String Saal = "saal";
private static final String Title = "title";
//private static final String EXAM_NUM = "exam_num";
private static final String KONKOOR_NUM = "konkoor_num";
private static final String QUESTION_NUM = "question_num";
private static final String USEER_ANSWER = "user_answer";
private static final String TAG = "DataBase";
public DataBase(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_FAVOURITE_GHAVAED_TABLE = "CREATE TABLE " + TABLE_FAVOURITE_GHAVAED + "("
+ Saal + " TEXT,"
+ Title + " TEXT" + ")";
String CREATE_FAVOURITE_QUESTIONS_TABLE = "CREATE TABLE " + TABLE_FAVOURITE_QUESTIONS + "("
+ KONKOOR_NUM + " TEXT,"
+ QUESTION_NUM + " TEXT" + ")";
String CREATE_USER_EXAM_TABLE = "CREATE TABLE " + TABLE_USER_EXAM + "("
+ KONKOOR_NUM + " TEXT,"
+ QUESTION_NUM + " TEXT,"
+ USEER_ANSWER + " TEXT" + ")";
String CREATE_EXAM_FINAL_PERCENT = "CREATE TABLE " + TABLE_EXAM_FINAL_PERCENT + "("
+ KONKOOR_NUM + " TEX" + ")";
db.execSQL(CREATE_FAVOURITE_GHAVAED_TABLE);
db.execSQL(CREATE_FAVOURITE_QUESTIONS_TABLE);
db.execSQL(CREATE_USER_EXAM_TABLE);
db.execSQL(CREATE_EXAM_FINAL_PERCENT);
}
@Override
//user answer is a set get class of exam year and question numbers
public void deleteUser(User_answer user_answer) {
SQLiteDatabase db = this.getWritableDatabase();
String[] args = new String []{ user_answer.getKonkoor_num(), user_answer.getQuestion_num()};
db.delete(TABLE_USER_EXAM, " konkoor_num =? AND question_num=?", args);
Log.i("getUserExam", "deleting done!");
db.close();
}
以下是我的考试成绩列表视图类,所有年份和问题编号的答案都在此列表中:
public class pasokh_azmoon extends AppCompatActivity {
List<User_answer> list;
Button delete_exam;
User_answer us;
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pasokh_azmoon);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setTitle("پاسخ آزمون");
final DataBase db = new DataBase(this);
list = db.getAnswer();
final ListView listView = (ListView) findViewById(R.id.pasokh_azmon_listview);
final pasokh_azmoon_adapter adapter = new pasokh_azmoon_adapter(getApplicationContext(), list);
listView.setAdapter(adapter);
//BUTTON I USED OUTSIDE THE LIST VIEW TO DELETE LIST
delete_exam = (Button) findViewById(R.id.delete_exam);
delete_exam.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//this is where I get the error
us.setKonkoor_num();
us.setQuestion_num();
db.deleteUser(us);
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
db.deleteUser_Exam(list.get(position).getKonkoor_num(), list.get(position).getQuestion_num());
}
});
}
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if (id == android.R.id.home) {
finish();
}
return true;
}
}