I am trying to set visibility of two imageviews inside a custom listview based on the value in a textView in the same listview. But, I am getting a NullPointerException
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setVisibility(int)' on a null object reference
on the line
iv_in_time.setVisibility(View.INVISIBLE);
My Java file is:
public class Attendance extends AppCompatActivity {
ImageButton ib_clear_srch;
EditText et_srch;
TextView tv_emp_name, tv_emp_status, tv_emp_in_time, tv_emp_out_time;
ImageView iv_in_time, iv_out_time, iv_pop_down, iv_emp_img;
ArrayList<EmpAttenModel> empAttenModelArrayList;
private EmpAttenAdapter empAttenAdapter;
ListView lv_emp_attendance;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attendance);
et_srch = (EditText)findViewById(R.id.et_srch);
ib_clear_srch = (ImageButton)findViewById(R.id.ib_clear_srch);
ib_clear_srch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
et_srch.setText(null);
}
});
View v;
tv_emp_name = (TextView)findViewById(R.id.tv_emp_name);
tv_emp_status = (TextView)findViewById(R.id.tv_emp_status);
tv_emp_in_time = (TextView)findViewById(R.id.tv_emp_in_time);
tv_emp_out_time = (TextView)findViewById(R.id.tv_emp_out_time);
iv_in_time = (ImageView)findViewById(R.id.iv_in_time);
iv_out_time = (ImageView)findViewById(R.id.iv_out_time);
iv_pop_down = (ImageView)findViewById(R.id.iv_pop_down);
iv_emp_img = (ImageView)findViewById(R.id.iv_emp_img);
lv_emp_attendance = (ListView)findViewById(R.id.lv_emp_atten);
empAttenModelArrayList = new ArrayList<>();
empAttenModelArrayList.add(new EmpAttenModel("Ravi Sharma", "Status-Absent","In time", "Out time"));
empAttenModelArrayList.add(new EmpAttenModel("Ravi Sharma", "Status-Working","9:30 AM", "Out time"));
empAttenModelArrayList.add(new EmpAttenModel("Ravi Sharma", "Status-Present","9:30 AM", "5:00 PM"));
empAttenAdapter = new EmpAttenAdapter(empAttenModelArrayList,getApplicationContext());
lv_emp_attendance.setAdapter(empAttenAdapter);
TextView tv1;
for (int i = 0; i < lv_emp_attendance.getCount(); i++){
v = lv_emp_attendance.getAdapter().getView(i, null, null);
tv1 = (TextView)v.findViewById(R.id.tv_emp_status);
if(tv1.getText().toString().equals("Status-Absent")){
iv_in_time.setVisibility(View.INVISIBLE);
iv_out_time.setVisibility(View.INVISIBLE);
tv_emp_in_time.setVisibility(View.INVISIBLE);
tv_emp_out_time.setText("In time");
}
if(tv1.getText().toString().equals("Status-Working")){
iv_out_time.setVisibility(View.INVISIBLE);
}
}
}
}
Can anyone tell me how to solve this issue?
It seems it doesn't find the view with the id you provide and so your view get to be null and you can't make operations on it (other than check if it's null or not).
If both the imageviews and the textview you wanna work on are part of the layout of the row of your listview, as I believe, I think it would be easier to do you check directly in the adapter, where you create the view and set the right values.
Checking for the values and views in the activity just complicates things. You can do the check you're already doing in the adapter, where you have all your views correctly initialised.