when Im running this code in a CustomListView with the checkbox onclicklistener Im getting a NullPointException, how is this possible, how can i fix it? I took this code from an example, the example alone worked fine, but mine crashes all the time.
private class MyCustomAdapter extends ArrayAdapter<Country> {
private ArrayList<Country> countryList;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<Country> countryList) {
super(context, textViewResourceId, countryList);
this.countryList = new ArrayList<Country>();
this.countryList.addAll(countryList);
}
private class ViewHolder {
TextView code;
CheckBox name;
TextView Beschreibung, FachDatum;
CheckBox cbStatus;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.tabitem, null);
holder = new ViewHolder();
holder.code = (TextView) convertView.findViewById(R.id.code);
holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
holder.Beschreibung = (TextView) convertView.findViewById(R.id.tvBeschreibung);
holder.FachDatum = (TextView) convertView.findViewById(R.id.tvFachDatum);
holder.cbStatus = (CheckBox) convertView.findViewById(R.id.cbtabitemerledigt);
convertView.setTag(holder);
holder.cbStatus.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Country country = (Country) cb.getTag();
Toast.makeText(getApplicationContext(),
"Clicked on Checkbox: " + cb.getText() +
" is " + cb.isChecked(),
Toast.LENGTH_LONG).show();
country.setSelected(cb.isChecked()); //this line is causing the crash
}
});
}
else {
holder = (ViewHolder) convertView.getTag();
}
Country country = countryList.get(position);
holder.cbStatus.setChecked(country.isSelected());
holder.cbStatus.setText("abc");
holder.Beschreibung.setText(country.getBeschreibung());
holder.FachDatum.setText(country.getFachDatum());
return convertView;
}
}
Here is the LogCat:
12-20 21:30:24.247 11376-11376/com.example.mathias.hausaufgabenplaner E/AndroidRuntime: FATAL EXCEPTION: main
12-20 21:30:24.247 11376-11376/com.example.mathias.hausaufgabenplaner E/AndroidRuntime: Process: com.example.mathias.hausaufgabenplaner, PID: 11376
12-20 21:30:24.247 11376-11376/com.example.mathias.hausaufgabenplaner E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.mathias.hausaufgabenplaner.CLV.Country.setCBState(boolean)' on a null object reference
12-20 21:30:24.247 11376-11376/com.example.mathias.hausaufgabenplaner E/AndroidRuntime: at com.example.mathias.hausaufgabenplaner.HUActivity$MyCustomAdapter$1.onClick(HUActivity.java:343)
12-20 21:30:24.247 11376-11376/com.example.mathias.hausaufgabenplaner E/AndroidRuntime: at android.view.View.performClick(View.java:4785)
12-20 21:30:24.247 11376-11376/com.example.mathias.hausaufgabenplaner E/AndroidRuntime: at android.widget.CompoundButton.performClick(CompoundButton.java:125)
12-20 21:30:24.247 11376-11376/com.example.mathias.hausaufgabenplaner E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:19858)
12-20 21:30:24.247 11376-11376/com.example.mathias.hausaufgabenplaner E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
12-20 21:30:24.247 11376-11376/com.example.mathias.hausaufgabenplaner E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
12-20 21:30:24.247 11376-11376/com.example.mathias.hausaufgabenplaner E/AndroidRuntime: at android.os.Looper.loop(Looper.java:155)
12-20 21:30:24.247 11376-11376/com.example.mathias.hausaufgabenplaner E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5696)
12-20 21:30:24.247 11376-11376/com.example.mathias.hausaufgabenplaner E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
12-20 21:30:24.247 11376-11376/com.example.mathias.hausaufgabenplaner E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
12-20 21:30:24.247 11376-11376/com.example.mathias.hausaufgabenplaner E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
12-20 21:30:24.247 11376-11376/com.example.mathias.hausaufgabenplaner E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)