0

I'm trying to use a getter for an object, in order to store the information of that object in another object. Yet, just by using the getter, it immediately returns a NullPointerException.

This is the code of the class that has the object as a field (and the getter):

public class NodoAnos implements Serializable
{
private int ano;
private NodoAnos proximo;
private ListaEquipos equipos;

public NodoAnos(int año) 
{
    this.ano = año;
    this.proximo = null;
    this.equipos = new ListaEquipos();
}

public int getAno() 
{
    return ano;
}

public void setAno(int año) 
{
    this.ano = año;
}

public ListaEquipos getEquipos()
{
    return equipos;
}

Here are all the methods before the NullPointerException (exception in code sample 6):

Code Sample 1:

private void informacion() throws IOException, FileNotFoundException, ClassNotFoundException
{
    int ano = elegirAnoPopup(); //SEE CODE SAMPLE NUMBER 2
    NodoAnos nodAno = new NodoAnos(ano);

    nodAno = anos.buscarAños(nodAno); //SEE CODE SAMPLE NUMBER 5

    datosComboBoxUniversidad(nodAno); //SEE CODE SAMPLE NUMBER 6
}

Code Sample 2:

private int elegirAnoPopup() throws IOException, FileNotFoundException, ClassNotFoundException
{
    NodoAnos aux = new NodoAnos(1);
    List<String> choices = new ArrayList<>();

    anos = anos.leerArchivo(anos); //SEE CODE SAMPLE NUMBER 3

    while(aux != null)
    {
        aux = anos.llenarDialogo(aux); //SEE CODE SAMPLE NUMBER 4

        if(aux != null)
            choices.add(Integer.toString(aux.getAno()));
    }

    ChoiceDialog<String> dialog = new ChoiceDialog<>("Elegir año",choices);
    dialog.setTitle("Consultar Informacion");
    dialog.setHeaderText("Por favor, introduzca la informacion requerida");
    dialog.setContentText("Introduzca el año a consultar:");

    Optional<String> result = dialog.showAndWait();
    return Integer.parseInt(result.get());
}

Code Sample 3:

public ListaAnos leerArchivo(ListaAnos anos) throws FileNotFoundException, IOException, ClassNotFoundException
{
    ListaAnos lista = anos;

    try
    {
        FileInputStream fis = new FileInputStream("lista.DAT");
        ObjectInputStream ois = new ObjectInputStream(fis);

        if(ois != null)
        {
            ListaAnos objeto = (ListaAnos) ois.readObject();
            lista = objeto;

            ois.close();
        }
    }
    catch(FileNotFoundException e)
    {
        System.out.println("No existe un archivo");
    }

    return lista;
}

Code Sample 4:

public NodoAnos llenarDialogo(NodoAnos aux)
{
    if(aux.getAno() == 1)
        aux = cabeza;
    else
        aux = aux.getProximo();

    return aux;
}

Code Sample 5:

public NodoAnos buscarAños(NodoAnos nodAño)
{
    NodoAnos aux = cabeza;

    while(aux != null)
    {
        if( nodAño.getAno() == aux.getAno() )
        {
            return aux;
        }
        else
        {
            aux = aux.getProximo();
        }
    }

    return null;
}

Code Sample Number 6:

private void datosComboBoxUniversidad(NodoAnos nodAno)
{
    ListaEquipos listaEquipo = new ListaEquipos();
    NodoEquipos nodEquipo = new NodoEquipos(null, 0, 0);

    listaEquipo = nodAno.getEquipos(); //NULLPOINTEREXCEPTION

    while(nodEquipo != null)
    {
        nodEquipo = listaEquipo.buscarEquipos(nodEquipo);

        if(nodEquipo != null)
        {
            comboUniversidad.getItems().add(nodEquipo.getUniversidad());
        }
    }

}

IMPORTANT NOTE: nodAno is not Null. Already made sure of it. it prints as follows: torneodetenis.NodoAnos@12539123

GFV450
  • 111
  • 1
  • 11

1 Answers1

1

The parameter you are passing in nodAno must be null. In Java, calling a method on a null object will result in a NullPointerException.

Just a small tip to avoid these issues: If you use IntelliJ IDEA or a similar IDE you can add the @NotNull annotation to your parameter and the IDE will warn you if you are passing a possibly null object into a method call that requires a non-null value. You can read more about this here: https://www.jetbrains.com/idea/help/nullable-and-notnull-annotations.html

aeskreis
  • 1,693
  • 2
  • 16
  • 22
  • nodAno isn't null. I've made sure of it. – GFV450 Nov 18 '15 at 22:20
  • Please edit your question to include the full stack trace. With the information given, that is the only explanation. – aeskreis Nov 18 '15 at 22:21
  • Whole code up to the NullPointerException added – GFV450 Nov 18 '15 at 22:31
  • This is helpful, but you still did not include the stack trace, which also contains very useful information. – aeskreis Nov 19 '15 at 19:31
  • There is definitely a code path which can lead to nodAno being null. Since Code Sample 5 returns null if none of the other return conditions are met. Maybe try having your final case not return null, but instead return an empty value? – aeskreis Nov 19 '15 at 19:34