Java, Exception in thread & ldquo; hand & rdquo; java.lang.NullPointerException caused by no initialization of an object

advertisements

Help me with this, "Exception in thread "main" java.lang.NullPointerException" thanks

private List< PosibleTerreno> posibles_terrenos;
private List< PosibleTerreno> terrenos_validos;

//-------------------------------

 int cantidad = this.posibles_terrenos.size(); 

        for (int i = 0 ; i < cantidad ; i++)
        {
            if(this.posibles_terrenos.get(i).get_validez() == true)
            {
                this.terrenos_validos.add(this.posibles_terrenos.get(i));
            }
        }


You have declared these variables

private List< PosibleTerreno> posibles_terrenos;
private List< PosibleTerreno> terrenos_validos;

but you have not initialized them. You need to do something along the lines of

private List< PosibleTerreno> posibles_terrenos = new ArrayList<PosibleTerreno>();
private List< PosibleTerreno> terrenos_validos = new ArrayList<PosibleTerreno>();

Otherwise, both lists are null, and attempting to reference any of their functions...doesn't even make sense, because there's no "their" there. They're nothing. So attempting this

int cantidad = this.posibles_terrenos.size();

will obviously result in a NullPointerException.

(+1 for three homophones in a row.)