0

There's a

      java.lang.NullPointerException: Attempt to invoke virtual method  
      'void androidx.recyclerview.widget.RecyclerView.setLayoutManager
      (androidx.recyclerview.widget.RecyclerView$LayoutManager)' 

on a null object reference and the cause is HomeFragment line 62 and Feed line 100.

I tried to change my to change my recycle view into android.support.v7.widget.RecyclerView and the error is now feed line 43 but that is the location of my recyclerview.

I also tried to change the fragment line 63 to getActivity() instead of context.getContext() This is my Feed class

Line 43:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_feed);

Line 100:

    @Override
    protected void onStart() {
    super.onStart();

}

Fragment

Line 63:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_home, container, false);

    blog_list = new ArrayList<>();
    user_list = new ArrayList<>();
    blog_list_view = view.findViewById(R.id.blog_list_view);

    blogRecyclerAdapter = new BlogRecyclerAdapter(blog_list, user_list);
    blog_list_view.setLayoutManager(new 
    LinearLayoutManager(container.getContext()));
    blog_list_view.setAdapter(blogRecyclerAdapter);
    blog_list_view.setHasFixedSize(true);

this is my layout on feed

    <androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Feed">

    <androidx.appcompat.widget.Toolbar
    android:id="@+id/main_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/authui_colorPrimary"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"> 
   </androidx.appcompat.widget.Toolbar>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/add_post_btn"
    android:layout_width="190dp"
    android:layout_height="59dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="16dp"
    android:clickable="true"
    android:focusable="true"
    app:layout_constraintBottom_toTopOf="@+id/feedBottomNav"
    app:layout_constraintEnd_toEndOf="parent"
    app:srcCompat="@android:drawable/ic_input_add" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/feedBottomNav"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:menu="@menu/bottom_menu" />

    <FrameLayout
    android:id="@+id/feed_container"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toTopOf="@+id/feedBottomNav"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/main_toolbar">

    <androidx.recyclerview.widget.RecyclerView
    android:id="@+id/blog_list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
    </FrameLayout>


    </androidx.constraintlayout.widget.ConstraintLayout>

What is the error on my code?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Neflheim
  • 1
  • 2
  • I assume the problem is the object on which you are trying to call `setLayoutManager`. The object is `blog_list_view` and this is set by looking up an existing object using `view.findViewById()`. I would recommend putting in a test when this var is set to see if it has fetched a valid object; if it has not then you are not passing a valid ID to it. – halfer May 07 '19 at 19:49
  • This line `view.findViewById(R.id.blog_list_view);` is most likely returning null. Make sure the id is correct and that it's under the view. Is it part of `fragment_home`? – Fred May 07 '19 at 20:53
  • thanks guys i already got the error the problem is the home fragment layout i didn't include it thank you – Neflheim May 08 '19 at 01:39

0 Answers0