-1

I have only the mainActivity and I use 3 fragnments and navigate through them with a bottomnav. All good until now, Im able to run the app on the emulator but when I select this fragment with my RecyclerView I get this error message and the app crashes

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference

I've seen a lot of alike answers and try to make my way arround but no success, so Im thinking Im not putting the code in the correct way or in the correct places, can you give me some advice?

Here is the Fragment code

public class administrador_atletas extends Fragment {

//Lista de atletas
public List<lista_atletas> lista_atl;
public RecyclerView rcc_lista_atletas;
public lista_atletas_adaptador adaptador_lista_atletas;



public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_administrador_atletas, container, false);
    rcc_lista_atletas = (RecyclerView)view.findViewById(R.id.recycler_administrador_atletas);
    return view;
}


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

    LinearLayoutManager linear = new LinearLayoutManager(this.getActivity());
    linear.setOrientation(LinearLayoutManager.VERTICAL);
    rcc_lista_atletas.setLayoutManager(linear);
}


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // do your variables initialisations here except Views!!!

    data();
    iniciar_adaptador_atletas();
}


public void onViewCreated(View view, Bundle savedInstanceState){
    super.onViewCreated(view, savedInstanceState);

}

public void data(){
    lista_atl = new ArrayList<>();
    lista_atl.add(new lista_atletas("Astrid Ruvalcaba Ramos", "Esgrima"));
    lista_atl.add(new lista_atletas("Daniel Sanchez Cuevas", "G. Artistica"));
    lista_atl.add(new lista_atletas("Alexa Luna Contreras", "TKD"));
    lista_atl.add(new lista_atletas("Paul Carillo Mendez", "Natacion"));
    lista_atl.add(new lista_atletas("Karen Mendoza Galindo", "Boxeo"));
    lista_atl.add(new lista_atletas("Marco Torres Miranda", "Tiro con arco"));
}

public void iniciar_adaptador_atletas(){
    adaptador_lista_atletas = new lista_atletas_adaptador(lista_atl);
    rcc_lista_atletas.setAdapter(adaptador_lista_atletas);
}

Thanks in advance

EDIT: I just moved

 data();
 iniciar_adaptador_atletas();

Bellow

rcc_lista_atletas.setLayoutManager(linear);

In onCreateView so I have this

 public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_administrador_atletas, container, false);

    rcc_lista_atletas = (RecyclerView)view.findViewById(R.id.recycler_administrador_atletas);

    LinearLayoutManager linear = new LinearLayoutManager(this.getActivity());
    linear.setOrientation(LinearLayoutManager.VERTICAL);
    rcc_lista_atletas.setLayoutManager(linear);
    data();
    iniciar_adaptador_atletas();
    return view;
}

And it worked, now Im able to enter the fragment with my data

Many thanks to all, your info was very useful!

Angel R
  • 143
  • 1
  • 7
  • https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Gaurav Chauhan Oct 21 '18 at 04:19
  • DO not load data inside `onCreate()` . See [Fragments lifecycle](https://stackoverflow.com/a/36340059/4168607). You can load data inside `onViewCreated()`. – ADM Oct 21 '18 at 06:58

2 Answers2

0

As the error message says, you are calling the method setAdapter on a null-valued variable (probably rcc_lista_atletas). While the error is not exactly in the source code you published (you should update your post with the full code), I suppose one of the methods, data() or iniciar_adaptador_atletas(), is calling 'setAdapter'.

You must remember that onCreate is executed before onCreateView. So, you're probably calling setAdapter before the onCreateView is executed and, doing so, rcc_lista_atletas is still null. Move data() and iniciar_adaptador_atletas() to the line before "return view;" in onCreateView and test it again.

This is the best we can do without checking you full code.

Leandro Luque
  • 518
  • 3
  • 6
0

It seems, you try to connect adapter to recyclerView before created recyclerView. So, try to move iniciar_adaptador_atletas(); to the bottom of

rcc_lista_atletas = (RecyclerView)view.findViewById(R.id.recycler_administrador_atletas);
Sinan Ceylan
  • 1,043
  • 1
  • 10
  • 14