NullPointerException for mSolvedImageView

Final Edit: This has been solved. I carelessly forgot to account for the XML made in challenge 8.1, as Jinisha and Nitrodon pointed out. Thanks everyone for the help. Happy Learning

I’m running into a weird error. I’m on page 199, where we are updating the ImageView so that the handcuffs are only shown on crimes that have been solved. At first I did everything the book asked, such as change the image ID to crime_solved, added the mSolvedImageView member to CrimeHolder, then initialized the ImageView in the CrimeHolder constructor. Then after running i into errors I realized, thanks to a post made on this forum, that crime_solved has been used as an ID in chapter 7. So like the forum post said, I changed the ID once again to solvedImageView to fix the clashing IDs. But for some reason, the mSolvedImageView variable wont initialize in the CrimeHolder constructor even after giving it a unique ID. So the NullPointerException occurs in the bind method, when I attempt to mSolvedViewImage.setVisibility(crime.isSolved() ? View.VISIBLE : View.GONE). When I run the debugger it just stays null, despite mTitleTextView and mDateTextView being initialized just fine. I don’t understand why. Please help. Here’s a part of my logcat:

2019-01-09 15:09:40.265 13299-13299/com.bignerdranch.android.criminalintent E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.bignerdranch.android.criminalintent, PID: 13299
java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.widget.ImageView.setVisibility(int)’ on a null object reference
at com.bignerdranch.android.criminalintent.CrimeListFragment$CrimeHolder.bind(CrimeListFragment.java:62)
at com.bignerdranch.android.criminalintent.CrimeListFragment$SeriousCrimeHolder.bind(CrimeListFragment.java:84)
at com.bignerdranch.android.criminalintent.CrimeListFragment$CrimeAdapter.onBindViewHolder(CrimeListFragment.java:121)
at com.bignerdranch.android.criminalintent.CrimeListFragment$CrimeAdapter.onBindViewHolder(CrimeListFragment.java:96)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4194)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)

Edit: As requested by the two comments below, here’s part of my CrimeListFragment.java. I also deleted some of the seemingly irrrelevant error logcat info above. I can only post one image since I’m a new user, unfortunately. I hope this part is relevant.

are you able to post any of your code for these relevant areas?

Things you can check

Check your Crime.java file to see if the getter is isSolved() or ismSolved()

Also, are you setting the crime received in mCrime in the bind method.

Example:
public void bind(Crime crime){
mCrime crime;

and then set it as mSolvedImageView.setVisibility(mCrime.ismSolved() View.VISIBLE View.GONE);

If you can post your CrimeListFragment.java file here, then I can replicate the issue and revert accordingly.

1 Like

I have posted a snippet of my CrimeListFragement class. Hope this helps.

I edited my post to include what seems to be the relevant part of my CrimeListFragment class. Please let me know if there are any other parts that need posting. As a new user I can only post one screenshot at a time.

at com.bignerdranch.android.criminalintent.CrimeListFragment$CrimeHolder.bind(CrimeListFragment.java:62)
at com.bignerdranch.android.criminalintent.CrimeListFragment$SeriousCrimeHolder.bind(CrimeListFragment.java:84)
at

need to see line 62 and line 84 and the Crime.java file for the getter isSolved.

1 Like

LINE 61 - m is missing in crime.isSolved

mSolvedImageView.setVisibility(mCrime.isSolved() ? View.VISIBLE : View.GONE);

1 Like

Can you post your XML layouts for list_item_crime and serious_crime? It looks like one (or both) of those is missing a solvedImageView.

1 Like

I changed it to mCrime, thinking this was the only problem. But for some reason I’m still getting a NullPointerException:

Error - same null pointer then we need to check the layouts…

Here’s my list_item_crime XML:

Here’s my serious_crime XML, I think this is the problem. I overlooked the fact that for serious crimes I need to include solvedImageView. My list_item_crime XML has just been posted in response to u/Nitrodon:

yes please put that in serious crime

1 Like

That worked! Thank you so much, it was a careless error.

It was the XML for my serious_crime file, thank you.

gr8 Job! Happy Coding!