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));
            }
        }
nabster
  • 1,561
  • 2
  • 20
  • 32
  • 2
    Read [What is a Null Pointer Exception?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception). – PakkuDon Mar 02 '14 at 03:30
  • 1
    Make sure you have `posibles_terrenos = new ...` and same for `terrenos_validos` somewhere before executing the line throwing exceptiion – Karthik Kalyanasundaram Mar 02 '14 at 03: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. – keshlam Mar 02 '14 at 03:31
  • 1
    You have to make sure posibles_terrenos and terrenos_validos are correctly initialized. – Ale Zalazar Mar 02 '14 at 03:33
  • private List< PosibleTerreno> posibles_terrenos; private List< PosibleTerreno> terrenos_validos; it's defined next to " public class Controlador { " – Cardona Jeison Mar 02 '14 at 03:33
  • Lists are objects in Java and you need to initialize them as many commenters have pointed out. You cannot do something (in your case .get(i).get_validez()) on object members without initializing them. – nabster Mar 02 '14 at 03:35
  • :D initialized in in the constructor thanks – Cardona Jeison Mar 02 '14 at 03:36
  • Then it´s not enough information to determine the origin of the error. Could you post some more of your code? – Ale Zalazar Mar 02 '14 at 03:41

3 Answers3

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

Community
  • 1
  • 1
aliteralmind
  • 19,847
  • 17
  • 77
  • 108
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<>();
Community
  • 1
  • 1
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
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>();
tkt986
  • 1,081
  • 3
  • 17
  • 25