Enum me returning null Pointer

Asked

Viewed 91 times

0

My Enum is returning Null Pointer when re-running this call:

  System.out.println(DefinicaoSCM.getDefinicaoSCMPorDisciplina(Disciplina.IMPLEMENTACAO));

Implementation:

public enum DefinicaoSCM
{
   DESCONHECIDO(null, "NA", "NA"),
   IMPLANTACAO(Disciplina.IMPLANTACAO, "ipl_", "/Implantacao"),
   IMPLEMENTACAO(Disciplina.IMPLEMENTACAO, "imp_", "/Implementacao", "/Documentacao", "/Implantacao"),
   INTERFACE_GRAFICA(Disciplina.INTERFACE_GRAFICA, "ifg_", "/Design"),
   PROJETO(Disciplina.PROJETO, "prj_", "/Projeto"),
   REQUISITO(Disciplina.REQUISITO, "req_", "/Requisitos"),
   TESTE(Disciplina.TESTE, "tst_", "/Teste");

   private static Map<Disciplina, DefinicaoSCM> definicaoSCMPorDisciplina;
   private Disciplina disciplina;



   public static DefinicaoSCM getDefinicaoSCMPorDisciplina(Disciplina disciplina)
   {
      return definicaoSCMPorDisciplina.get(disciplina);
   }
  • 2

    definicaoSCMPorDisciplina is not instantiated.

  • Can you post your entire code from this class? In particular I would like to know about the constructor and how the definicaoSCMPorDisciplina is instantiated and populated.

  • Although I don’t have the complete code, I’ve had problems using a Map in an Enum before. I suggest trying with an Immutablemap from Guava. I’ll find what I did to solve and put the code in after.

1 answer

0


To use a Map in an Enum, I could only use the Immutablemap of Guava, in your example, would be as below:

private static final Map<Disciplina, DefinicaoSCM> definicaoSCMPorDisciplina = ImmutableMap.<Disciplina, DefinicaoSCM>builder()
                    .put(Disciplina.IMPLANTACAO, new DefinicaoSCM())
                    .put(Disciplina.INTERFACE_GRAFICA, new DefinicaoSCM())
                    .put(Disciplina.PROJETO, new DefinicaoSCM())
                    .put(Disciplina.REQUISITO, new DefinicaoSCM())
                    .put(Disciplina.TESTE, new DefinicaoSCM())
                    .build();

Access to the Map continues in the same way .get(key).

  • Caracas, very good! Thank you very much my friend!

Browser other questions tagged

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