List null android

0

I have a problem in the list, in the init() method that is triggered as soon as I enter the screen it has the following code:

listaOpcionais = Globales.getListaOpcionais();

At this moment I list my list in the globals, but the first time I start the screen, it points my list to the value null . This gives me problems when, for example, I'm going to add an item:

listaOpcionais.add(opcao);

How do I stop an empty list at this point if I return null ? In my class Globales I'm setting 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>();
    
asked by anonymous 31.05.2017 / 14:43

1 answer

1

To prevent it from returning null, make sure the list was not instantiated.

public static List<OpcaoPedido> getListaOpcionais() {
 if(listaOpcionais == null){
     return new ArrayList<OpcaoPedido>();
 }
     return listaOpcionais; 
 }
    
31.05.2017 / 14:50