Chapter challenge - How to wire up contact police button

Am able to setup a different view for the RecyclerView to show a contact police button when is selected in the crime object.

However, I am at a loss how to wire this button up? I get a Null Pointer exception when I try to either set this in the constructor of the ViewHolder, or in the bind method…

                                             java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

Here is the code as implemented in my bind method.

    public void bind (Crime crime) {
        mCrime = crime;
        if (mCrime.isPoliceRequired()) {
            mContactPoliceButton.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(getActivity(), "Police Contacted", Toast.LENGTH_SHORT).show();

any help is welcome!

Your code is crashing because mContactPoliceButton is null. You are calling setOnClickListener on that null object.

The fix is to make sure that you are using findViewById to assign a value to mContactPoliceButton. Take a look at where you use findViewById to set a value for mTitleTextView and repeat that pattern with the new button.

You’re absolutely correct… Mea culpa, I wired up the button, but to the wrong resource… Oops. Thanks for your help.