1

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));
            }
        }
8
  • 2
    Read What is a Null Pointer Exception?. Commented Mar 2, 2014 at 3:30
  • 1
    Make sure you have posibles_terrenos = new ... and same for terrenos_validos somewhere before executing the line throwing exceptiion Commented Mar 2, 2014 at 3:31
  • And then look at the exception message and stack trace, which will tell you exactly which line of which file it occurred on and what you were doing at the time. Commented Mar 2, 2014 at 3:31
  • 1
    You have to make sure posibles_terrenos and terrenos_validos are correctly initialized. Commented Mar 2, 2014 at 3:33
  • private List< PosibleTerreno> posibles_terrenos; private List< PosibleTerreno> terrenos_validos; it's defined next to " public class Controlador { " Commented Mar 2, 2014 at 3:33

3 Answers 3

5

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.)

Sign up to request clarification or add additional context in comments.

Comments

3

You need to initialize your List(s), you might use a ArrayList and/or a LinkedList perhaps with -

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

With Java 7 and above, you can also use the diamond operator like so -

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

Comments

2

do this before executing the line throwing the exception.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.