0

Currently developing a mobile app using java with android studio. (Beginner) I have 3 fragments (ToDo,Doing,Done) with on top a tablayout(ToDo,Ongoing,Finished) and a toolbar with an add button.

The add button should create a list allowing the user to modify the content of each list(Recyclerviewer)

Obtain error: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference

Below is a screenshot

kanban

Code

public class kanbanManagement extends Fragment {

    // TODO: Rename and change types of parameters
    TabLayout tabLayout;
    private ViewPager2 viewPager2;
    private MyFragment adapter;
    private RecyclerView recyclerView2;
    private com.example.myhouse.recyclerKanbanAdapter recyclerKanbanAdapter;
    ArrayList<kanbanItems>listKanban;
    Button btn_Add;
    public kanbanManagement() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_kanban_management, container, false);
        tabLayout=view.findViewById(R.id.tabLayout);
        viewPager2=view.findViewById(R.id.viewPager2);
        tabLayout.addTab(tabLayout.newTab().setText("To Do"));
        tabLayout.addTab(tabLayout.newTab().setText("On going"));
        tabLayout.addTab(tabLayout.newTab().setText("Finished"));
        FragmentManager fragmentManager = getParentFragmentManager();
        adapter = new MyFragment(fragmentManager , getLifecycle());
        viewPager2.setAdapter(adapter);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager2.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                tabLayout.selectTab(tabLayout.getTabAt(position));
            }
        });
        recyclerView2=view.findViewById(R.id.taskList);
        listKanban= new ArrayList<>();
        btn_Add=view.findViewById(R.id.btnAdd);
        recyclerKanbanAdapter= new recyclerKanbanAdapter(getActivity(),listKanban);
        btn_Add.setOnClickListener(v -> {
            recyclerView2.setAdapter(recyclerKanbanAdapter);

        });

        return view;
    }
}
KernelPanic
  • 2,328
  • 7
  • 47
  • 90
  • `recyclerView2` is null, indicating that `findViewById` did not find a result. – Rogue May 26 '22 at 12:45
  • yes found that when debugging. Any solution? – yanish appadoo May 26 '22 at 13:00
  • Well you'll need to investigate why it cannot find a result. Run a debugger to determine the actual value of `R.id.taskList` when running your code, and see what the ID _should_ be. It more than likely is a mismatch. – Rogue May 26 '22 at 13:43
  • its 2 different xml layouts. am I correctly doing the findviewbyid – yanish appadoo May 26 '22 at 13:46
  • Can you provide the `layout.xml` of your views? I am also curious why you set the adapter on a button click. It could be set while creating the view, because its initial data is a empty list. – Daniel Knauf May 28 '22 at 05:21
  • @yanishappadoo add `LayoutManager` to manage your `recyclerViews`. How your recyclerView shows. – M DEV May 28 '22 at 05:35

1 Answers1

0

Sorry, I didn't fully understand your code. So, without checking your complete code. My guess is that you haven't used LayoutManager to show your listKanban. So add layoutManager code and run again.

Add the below code inside your onCreateView.

LinearLayoutManager myLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        recyclerView2.setLayoutManager(myLayoutManager); // your recyclerView where you want to show your `ArrayList`.

Let me know if the problem is not solved yet.

M DEV
  • 763
  • 1
  • 7
  • 20