-3

When I attempt to run my app I get an error that says I am attempting to invoke a method on a null object. I know where the error is occurring, but I am unsure as why since I thought I initialized the object earlier.
Code as follows:

public class InjuryListFragment extends Fragment{
    private RecyclerView mInjuryRecyclerView;
    private InjuryAdapter mAdapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view=inflater.inflate(R.layout.fragment_injury_list, container, false);
        mInjuryRecyclerView=(RecyclerView)view.findViewById(R.id.injury_recycler_view);
        mInjuryRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        updateUI();
        return view;
    }
    private void updateUI(){
        InjuryLab injuryLab=InjuryLab.get(getActivity());
        List<Injury>injuries=injuryLab.getInjuries();
        mAdapter=new InjuryAdapter(injuries);
        mInjuryRecyclerView.setAdapter(mAdapter);

    }
    private class InjuryHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        private TextView mTitleTextView;
        private TextView mDescriptionTextView;
        private Injury mInjury;
        public InjuryHolder(View itemView){
            super(itemView);
            itemView.setOnClickListener(this);
            mTitleTextView=(TextView)itemView.findViewById(R.id.list_item_injury_title_text_view);
            mDescriptionTextView=(TextView)itemView.findViewById(R.id.injury_description);
        }
        public void bindInjury(Injury injury){
            mInjury=injury;
            mTitleTextView.setText(mInjury.getTitle());
            mDescriptionTextView.setText(mInjury.getDescription());
        }
        @Override
        public void onClick(View v){
            Intent intent=InjuryActivity.newIntent(getActivity(), mInjury.getId());
            startActivity(intent);
        }
    }

The error occurs on the line mDescriptionTextView.setText(mInjury.getDescription());

Gaurav Gupta
  • 4,586
  • 4
  • 39
  • 72
Bukimon
  • 31
  • 1
  • 3
  • 1
    Tell us the error specifically, or even better post the logcat. Is the null object `mDescriptionTextView` or `mInjury`? – Breavyn Sep 23 '15 at 04:57
  • The logcat is as follows 09-23 00:03:37.106 19159-19159/com.bignerdranch.android.firstaid E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.bignerdranch.android.firstaid, PID: 19159 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.bignerdranch.android.firstaid.InjuryListFragment$InjuryHolder.bindInjury(InjuryListFragment.java:54) at com.bignerdranch.android.firstaid.InjuryListFragment$InjuryAdapter.onBindViewHolder(InjuryListFragment.java:79) – Bukimon Sep 23 '15 at 05:08
  • post your adapter code. – arun Sep 23 '15 at 05:14

2 Answers2

0

Double check the id of views in xml and in code.Check whether you are setting text before initializing the TextView.Also check whether the constructor is called before executing the bindInjury method

Jas
  • 3,207
  • 2
  • 15
  • 45
0

Try this

public void bindInjury(Injury injury){
  if(injury != null){
    mInjury=injury;
  }
  mTitleTextView.setText(mInjury.getTitle());
  mDescriptionTextView.setText(mInjury.getDescription());
}