Why does Null Exception give me?

0

My method is as follows:

public boolean addJugador(String nombre){
    boolean add=true;
    Jugador j= new Jugador(nombre);
        if(nombre!=mesa[jugadoresActuales].getNombre()){   

            mesa[jugadoresActuales]= j;
            jugadoresActuales++;
        }else{
        add=false;
        }

      return add;
}

The method getNombre is:

public String getNombre(){
    return nombre;

The player builder is

public Jugador(String nombre){
    this.nombre=nombre;
    manoJugador=new Carta[MAXCARTAS];
    cartasActuales=0;

Current players is initialized to 0. Mesa is a player-type array that keeps players added, and current players is the number of players. I do not understand at what point I'm pointing to null, any ideas?

    
asked by iHack 14.11.2017 в 13:51
source

3 answers

4

When it's the first player you add, current players equals 0, so if you do the comparison:

if(nombre!=mesa[jugadoresActuales].getNombre()){...}

It's normal for you to get a NullPointerException since you try to access table [0] that is null.

What you should do is, if it is the first player to add it to the array directly and if it is not, then if you do your comparison by name:

public boolean addJugador(String nombre){
    boolean add=true;
    Jugador j= new Jugador(nombre);
    if(jugadoresActuales==0){    //Si no hay jugadores lo añadimos
        mesa[jugadoresActuales]= j;
        jugadoresActuales++;
    }else if(nombre!=mesa[jugadoresActuales].getNombre()){   
        mesa[jugadoresActuales]= j;
        jugadoresActuales++;
    }else{
    add=false;
}

  return add;
}
    
answered by 14.11.2017 в 14:06
1

Your problem is that you are trying to check if there is a player without players in the Array mesa .

You must add a check to see if the array = null or if you do not have values to directly add the player and do not give the NullException :

public boolean addJugador(String nombre){
    boolean add=true;
    Jugador j= new Jugador(nombre);
    if (mesa != null && mesa.length != 0){
        if(nombre!=mesa[jugadoresActuales].getNombre()){   

            mesa[jugadoresActuales]= j;
            jugadoresActuales++;
        }else{
        add=false;
        }
    }else{
        //El array es null o no tiene valores, por lo tanto no existen jugadores y éste no va a existir, se añade
        mesa[jugadoresActuales]= j;
        jugadoresActuales++;
    }
      return add;
}
    
answered by 14.11.2017 в 14:06
0

Well in this code it works fine, only that you can enter duplicates of String, if it does not work for you I can go deeper. I did it a little in the air.

public class ControladorJugadores {

int jugadoresActuales = 0;
static Jugador[] mesa = new Jugador[3];

public boolean addJugador(String nombre) {
    boolean add = false;

    Jugador jugador = new Jugador(nombre);
    mesa[jugadoresActuales] = jugador;

    /* Agregamos acá los jugadores al array */

    if (nombre == mesa[jugadoresActuales].getNombre() && mesa[jugadoresActuales].getNombre() != null) {
        jugadoresActuales++;
        add = true;
    } else {
        add = false;
    }
    return add;
}

public static void main(String[] args) {
    ControladorJugadores mj = new ControladorJugadores();
    System.out.println("Agregando a " + mj.addJugador("Marcelo"));
    System.out.println("Agregando a " + mj.addJugador("Batistuta"));
    System.out.println("Agregando a " + mj.addJugador("Verinky"));

    System.out.println("Esta es la cantidad total al momento: " + mj.jugadoresActuales);

    for (Jugador item : mesa) {
        System.out.println(item.getNombre());
    }
}

}
    
answered by 15.11.2017 в 18:47