Nullpointerexception on a non-null object

Asked

Viewed 66 times

0

I’m doing a small project for graduation, in it I want to create a new character that has name, sex and class, I’m testing the program and it’s giving a very strange error.

Here’s the code:

// classe de teste
System.out.println("Digite o nome do seu personagem");
String nome = read.next();
System.out.println("Escolha o sexo\n"
        + "1. Masculino"
        + "2. Feminino");
int indicesexo = trataEntradas();
System.out.println("Escolha a classe\n"
        + "1. Guerreiro\n"
        + "2. Arqueiro\n"
        + "3. Mago\n"
        + "4. Clerigo\n"
        + "5. Ladino");
int indiceclasse = read.nextInt();
try{
    fachada.criaPersonagem(nome, indicesexo, indiceclasse);
}
catch (PersonagemJaCadastradoException e){
    System.out.println("Já existe um personagem com este nome!");
}

//fachada
public void criaPersonagem(String nome, int indicesexo, int indiceclasse) throws PersonagemJaCadastradoException {
    Personagem novoPersonagem = new Personagem(nome, indicesexo, indiceclasse);
    negociopersonagem.cadastrarPersonagem(novoPersonagem);
}

On the last line, (negociopersonagem.cadastrarPersonagem(novoPersonagem); the eclipse is accusing me NullPointerException, only that the object I am passing is not null.

Follows a print screen:

printScreen

How to fix? The code is not in error.

  • 1

    negociopersonagem was instated?

  • damn man ! What an idiot on my part, that was exactly the mistake, thank you for realizing so fast, hugging

  • Okay, I posted an answer just to let it logged as bug even, thank you

1 answer

0


His instance novoPersonagem of Personagem is not null, as we observe by the image.

However, negociopersonagem is a null reference. Then, to solve this, simply assign an instance to negociopersonagem somewhere in your class Fachada, something like that:

negociopersonagem = new NegocioPersonagem();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.