Before you guys make it duplicate, I have searched for it everywhere and I am stuck at a point where I need your expertise.
I am calling my data in a doInBackground method and storing the values in a SimpleAdapter and In the postExecute method, I am assigning the adapter to my gridView. And also in the PostExecute I have a searchView method that filters my adapter.
Now, when I press any letter in the search my App Crashes with a NullPointerException and if the same code I run on same thread without making an AsyncTask I do not get this error. Here is my code of DoInBackground:
protected Void doInBackground(Void... voids) {
try{
connect = CONN(un, passwords, db, ip);
String StoredProc = "{call pro.DocumentDelivery_inst_update_delete(?,?)}";
callableStatement=connect.prepareCall(StoredProc);
callableStatement.setString(1, "Select");
callableStatement.setString(2, MainActivity.usernam);
rs = callableStatement.executeQuery();
while(rs.next()){
Map<String,String> datanum = new HashMap<String, String>();
datanum.put("B",rs.getString("FullName"));
datanum.put("C",rs.getString("Name"));
datanum.put("D",rs.getString("DocumentType"));
datanum.put("A",rs.getString("Id"));
datanum.put("E",rs.getString("TaskType"));
datanum.put("F",rs.getString("Comments"));
datanum.put("G",rs.getString("Tasktime"));
datanum.put("H",rs.getString("Priority"));
datanum.put("I",rs.getString("Telephone"));
data.add(datanum);
}
String [] from = {"A","B","C","D","E","F","G","H","I"};
int[] views = {R.id.doc_grid_t4,R.id.doc_grid_t1, R.id.doc_grid_t2,R.id.doc_grid_t3,R.id.doc_grid_t5,R.id.doc_grid_t6,R.id.doc_grid_t7,R.id.doc_grid_t8,R.id.doc_grid_t9};
ADA = new SimpleAdapter(document_Pro_List.this,data,R.layout.custom_pro_document_view, from, views);
synchronized (this) {
int counter = 0;
while(counter<=4){
this.wait(100);
counter++;
publishProgress(counter*25);
}
}
}
catch (InterruptedException | SQLException e){
e.printStackTrace();
}
return null;
}
and this is my code for postExecute method:
protected void onPostExecute(Void result){
sv = (SearchView) findViewById(R.id.searchView);
progressDialog.dismiss();
gridView.setAdapter(ADA);
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String query) {
ADA.getFilter().filter(query);
//ADA.notifyDataSetChanged();
return false;
}
});
}
NOTE: the moment I take out this
Map<String,String> datanum = new HashMap<String, String>();
out of while loop I stop getting the NullPointerException BUT this does not help me because it then repeats the 1st data from the database to all the next ones. Also I know I should work with WebAPI and I am doing that side by side. Please help me out on how I can remove this Exception.
THANK YOU
So I figured it out, I sometimes had an empty value in my
datanum.put("D",rs.getString("DocumentType"));
and because this went Null the searchView couldn't handle that one Null value and gave a NullPointerException.