question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Null pointer exception with recyclerview

See original GitHub issue

I’m wrapping BottomSheetLayout around a LinearLayout that contains a RecyclerView (which is populated with RelativeLayouts). To test it, I’m calling

BottomSheetLayout bottomSheet = (BottomSheetLayout) v.findViewById(R.id.bottomsheet);
    bottomSheet.showWithSheetView(inflater.inflate(R.layout.fragment_layout, bottomSheet, false));

at the end of the fragment’s onCreateView(), after the recyclerview has already been set up.

However, this ends up breaking the RecyclerView, as any touch input causes this stack trace:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollVertically()' on a null object reference
        at android.support.v7.widget.RecyclerView.computeVerticalScrollOffset(RecyclerView.java:1540)
        at android.view.View.canScrollVertically(View.java:12835)
        at android.support.v4.view.ViewCompatICS.canScrollVertically(ViewCompatICS.java:35)
        at android.support.v4.view.ViewCompat$ICSViewCompatImpl.canScrollVertically(ViewCompat.java:1161)
        at android.support.v4.view.ViewCompat.canScrollVertically(ViewCompat.java:1575)
        at android.support.v4.widget.SwipeRefreshLayout.canChildScrollUp(SwipeRefreshLayout.java:643)
        at android.support.v4.widget.SwipeRefreshLayout.onInterceptTouchEvent(SwipeRefreshLayout.java:657)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1960)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2369)
        at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1719)
        at android.app.Activity.dispatchTouchEvent(Activity.java:2742)
        at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
        at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2330)
        at android.view.View.dispatchPointerEvent(View.java:8666)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4123)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3989)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
        at android.view.ViewRootImpl$AsyncInputStage.forward

Here’s a screenshot of what happens:

image

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
ZacSweerscommented, Sep 7, 2015

I don’t think this is a bug on our end. I can’t say for sure from looking at your stacktrace what’s wrong with it (I’m able to use recyclerview in a sheet just fine on an internal project).

I would hazard a guess though that it has something to do with SwipeRefreshLayout, which would almost certainly not work as desired with bottom sheet. Not too familiar with its implementation under the hood, but I could imagine a case where it tries to get a view under a certain x/y position and (perhaps mistakenly) assumes that result is never null. Try removing that wrapper and see if that helps.

0reactions
yanbin92commented, Mar 16, 2016

you use RecyclerView must use a LayoutManager, such as LinearLayoutManager layoutManager = new LinearLayoutManager(context); layoutManager.setOrientation(LinearLayoutManager.VERTICAL); mRecyclerView.setLayoutManager(layoutManager); i meet the same problem and solve the problem .

Read more comments on GitHub >

github_iconTop Results From Across the Web

RecyclerView with null pointer exception - Stack Overflow
Your RecyclerView is null. Are you sure you're looking for the correct id in the correct layout file? My guess is either you...
Read more >
NullPointerException in RecyclerView · Issue #585 - GitHub
RecyclerView item views actually store their ViewHolders in a custom LayoutParams class. So when we reset the LayoutParams ViewHolder reference ...
Read more >
How To Solve Errors & NullPointerExceptions | Android Studio
I had a number of requests on how to solve similar problems with Android RecyclerViews either having NullPointerExceptions, ...
Read more >
Android – RecyclerView with null pointer exception - iTecNote
I am getting some news data from my CMS,and want to display them in a RecyclerView.The problem is that I am getting a...
Read more >
[Resolved] attempt to invoke virtual method recyclerview ...
You have declared RecyclerView recyclerView; in your class, but you have not initialised it before trying to use it. So it is still...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found