Error in method: removerEditor

Asked

Viewed 35 times

0

I’m a beginner in Java, and I’m having an error in an Edit class method to remove the publisher. The method to remove an author works perfectly with the same logic, already in the remove editor method it gives this error:

inserir a descrição da imagem aqui

Follows the code:

Library class, containing the methods to remove Author and Publisher

    public void removerAutorAnterior(Autor codigo) {

        autores.remove(codigo);
    }


    public void removerEditoraAnterior(Editora codigo) {

     editora.remove(codigo);
    }

Edit class

case 1:{


    Autor AtorAnterior=biblioteca.pesquisarAutor(codigo);

    System.out.println("Digite o código do autor que deseja editar:");
    AtorAnterior.setCodigo(scannerNumerico.nextInt());


    if(AtorAnterior==null){
    System.out.println("Error: autor não encontrado");
   }

    else{
    System.out.println("Autor encontrado:"+AtorAnterior.getNome());

    Autor Autornovo= new Autor();

    System.out.println("Digite um novo nome:");
    Autornovo.setNome(scannerString.nextLine());

    System.out.println("Digite um novo email:");
    Autornovo.setEmail(scannerString.nextLine());

    biblioteca.removerAutorAnterior(AtorAnterior); **irá remover o autor anterior**

   biblioteca.cadastarAutor(Autornovo); **irá cadastrar o novo autor**
        }
            break;
        }   

That’s where the error in the method lies:removerEditoraAnterior(Editoraanterior):

    //Editar Editora
        case 2:{


            Editora EditoraAnterior=biblioteca.pesquisarEditora(codigo);

            System.out.println("Digite o código da editora que deseja editar:");
             EditoraAnterior.setCodigo(scannerNumerico.nextInt());

            if( EditoraAnterior==null){
                System.out.println("Error: Editora não encontrada");
            }

            else{
                System.out.println("Editora encontrada:"+ EditoraAnterior.getNome());

                Editora EditoraNova= new Editora();

                System.out.println("Digite os novos dados:\n");

                System.out.println("Digite novo nome:");
                EditoraNova.setNome(scannerNumerico.nextLine());

                System.out.println("Digite o novo cnpj");
                EditoraNova.setCnpj(scannerNumerico.nextInt());

                biblioteca.removerEditoraAnterior(EditoraAnterior);
                biblioteca.cadastrarEditora(EditoraNova);

            break;  
            }

            }
  • 1

    I’m beginning to question your IDE’s sanity, given the context of problems it has already presented. Clear the area and have a clean build

  • I agree with the above friend, I think his eclipse is a bit buggy, or there is another project in the eclipse that is conflicting with this one. Already checked the build path?

  • Man, you don’t put keys in after one case ._.

  • Friend always used keys after cases, and never gave any problem , I believe that the error ñ is because of this

  • I’m a beginner in Java/Eclipse, I don’t know how to clean the area , build, check build Patch or something like that yet ._.

1 answer

0

Guy may be a mistake in his eclipse. But this code is very strange. Try to feed this variable code with a value q does not exist. Then try to execute that line EditoraAnterior.setCodigo(scannerNumerico.nextInt()); There is no way to work it dude, how will you set a value to a null object? It will give Nullpointerexception has to put this test if( EditoraAnterior==null) after the line Editora EditoraAnterior=biblioteca.pesquisarEditora(codigo);

   Editora EditoraAnterior=biblioteca.pesquisarEditora(codigo);

   System.out.println("Digite o código da editora que deseja editar:");
   EditoraAnterior.setCodigo(scannerNumerico.nextInt());

   if( EditoraAnterior==null){
       System.out.println("Error: Editora não encontrada");
   }

Browser other questions tagged

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