-3

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)
Ribisl
  • 149
  • 1
  • 10

1 Answers1

1

Add the adapter code, have you set Country in getView method?

  • I edited the question, now you can see the custom adapter code – Ribisl Dec 21 '15 at 19:17
  • the the code with some updates https://gist.github.com/agustinsivoplas/8fe2ea805573158ac1b8 – AndroidRuntimeException Dec 22 '15 at 00:20
  • nice, it worked perfectly. Do you have an idea how i could do it, that if one checkbox or more is checked that a button appears? but it shouldn't dissapear if you remove a checked item from the list. I hope you know what I mean and you can give me an answer. – Ribisl Dec 22 '15 at 19:12
  • In the event of onClickListener of checkbox, button.setVisibility(View.GONE) or button.setVisibility(View.VISIBLE), you could use the method getVisibility() to avoid hide and show unnecessary. – AndroidRuntimeException Dec 23 '15 at 01:34
  • But with this the Button gets invisible if I uncheck a checked item, although there are still checked items – Ribisl Dec 23 '15 at 05:16