List null android

Asked

Viewed 81 times

0

I’m having trouble with the list, the method init() that is triggered as soon as I enter the screen has the following code:

listaOpcionais = Globales.getListaOpcionais();

At this point I list what has in globales, but the first time I initialize the screen, it arrow my list to the value null this gives me problems when for example I will add an item:

listaOpcionais.add(opcao);

How do I at this time arrow an empty list if return null ? In my class Globales I’m setando like this:

private static List<OpcaoPedido> listaOpcionais;

public static List<OpcaoPedido> getListaOpcionais() {
    return listaOpcionais;
}

public static void setListaOpcionais(List<OpcaoPedido> listaOpcionais) {
    Globales.listaOpcionais = listaOpcionais;
}

And in my class I start like this:

List<OpcaoPedido> listaOpcionais = new ArrayList<OpcaoPedido>();

1 answer

1


To prevent it from returning null, check that the list has not been instantiated.

public static List<OpcaoPedido> getListaOpcionais() {
 if(listaOpcionais == null){
     return new ArrayList<OpcaoPedido>();
 }
     return listaOpcionais; 
 }
  • This I place in my globales class?

  • Replaces your method public Static List<Optionrequest> getListaOptionais() { Return listOptional; }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.