Enum returns null from the database

Asked

Viewed 295 times

0

Hello, I’m implementing a DAO and doing some tests, I’m having some problems in enums. When running the test class the Enum Size returns null, but in the database I have records filled correctly.

Test class:

package br.com.caelum.jdbc.teste;

import java.util.List;

import br.com.caelum.jdbc.dao.ContatoDAO;
import br.com.caelum.jdbc.modelo.Contato;
public class TestaLista {
    public static void main(String[] args) {
        ContatoDAO contatoDAO = new ContatoDAO();

        List<Contato> contatos = contatoDAO.getLista();

        for (Contato contato : contatos) {
              System.out.println("Nome: " + contato.getNome());
              System.out.println("Email: " + contato.getEmail());
              System.out.println("Endereço: " + contato.getEndereco());
              System.out.println("Data de Nascimento: " + contato.getDataNascimento() + "\n");
              System.out.println("Tamanho: " + contato.getTamanho());
          }
    }
}

This is the Contact class, my main object and has an equivalent table in the database:

import java.time.LocalDate;
import java.time.LocalTime;

import br.com.caelum.jdbc.dao.Tamanho;

public class Contato {

    private Long id;
    private String nome;
    private String email;
    private String endereco;
    private LocalDate dataNascimento;
    private LocalTime horaAgendada;
    private Tamanho tamanho;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getEndereco() {
        return endereco;
    }

    public void setEndereco(String endereco) {
        this.endereco = endereco;
    }

    public LocalDate getDataNascimento() {
        return dataNascimento;
    }

    public void setDataNascimento(LocalDate dataNascimento) {
        this.dataNascimento = dataNascimento;
    }

    public LocalTime getHoraAgendada() {
        return horaAgendada;
    }

    public void setHoraAgendada(LocalTime horaAgendada) {
        this.horaAgendada = horaAgendada;
    }

    public Tamanho getTamanho() {
        return tamanho;
    }
    public void setTamanho(Tamanho tamanho) {
        this.tamanho = tamanho;
    }
}

My Enum Size (I’m just testing the bank’s returns, so I made an Enum just to test). I added toTamanho method to try to solve, because without it I could not even return, only the error below:

Exception in thread "main" java.lang.Nullpointerexception: Name is null at java.lang.Enum.valueOf(Enum.java:236) at br.com.Caelum.jdbc.dao.Tamanho.valueOf(Size.java:1) at br.com.Caelum.jdbc.dao.ContactoDAO.getLista(Contactodao.java:61) at br.com.Caelum.jdbc.teste.TestaLista.main(Testalista.java:17)

    package br.com.caelum.jdbc.dao;

public enum Tamanho {

    xSmall("x-small"),
    small("small"),
    medium("medium"), 
    large("large"),
    xLarge("x-large'");

    private String tamanho;

    Tamanho(final String tamanho){
        this.tamanho = tamanho;
    }

    public String getTamanho() {
        return tamanho;
    }

    public void setTamanho(String tamanho) {
        this.tamanho= tamanho;
    }

    @Override
    public String toString() {
        return tamanho;
    }



    public static Tamanho toTamanho(String value) {
        for (Tamanho tamanho : values()) {
            if (tamanho.equals(value)) {
                return tamanho;
            }
        }
        return null;
    }

}

And this is the result obtained after the execution of the class Testalist:

Nome: teste
Email: [email protected]
Endereço: Rua teste testeira
Data de Nascimento: 2018-02-21

Tamanho: null
Nome: teste
Email: [email protected]
Endereço: Rua teste testeira
Data de Nascimento: 2018-02-21

Tamanho: null
Nome: teste
Email: [email protected]
Endereço: Rua teste testeira
Data de Nascimento: 2018-02-21

Tamanho: null
Nome: teste
Email: [email protected]
Endereço: Rua teste testeira
Data de Nascimento: 2018-02-23

Tamanho: null
Nome: teste
Email: [email protected]
Endereço: Rua teste testeira
Data de Nascimento: 2018-02-23

Tamanho: null

a table print in the database:

inserir a descrição da imagem aqui

  • Well, in short I want to access the Enum in the bank. Even if it is otherwise. Vlw to everyone from now on!

1 answer

1


You can try that too:

// método getLista()

...

contato.setTamanho(Tamanho.valueOf(objetoResultSet.getString("size")));

In accordance with described as in your table has null value in some records it causes Nullpointerexception error when assigning on your Enum.

A way around this error could only search for records with size other than null.

  • select * from contatos where size != null
  • Então, eu tentei com valueOf mas recebi esse erro:&#xA;Exception in thread "main" java.lang.NullPointerException: Name is null at java.lang.Enum.valueOf(Enum.java:236) at br.com.caelum.jdbc.dao.Tamanho.valueOf(Tamanho.java:1) at br.com.caelum.jdbc.dao.ContatoDAO.getLista(Contactodao.java:61) at br.com.Caelum.jdbc.teste.TestaLista.main(Testalista.java:17) | The getLista code is this: https://pastebin.com/V2MZmfk7

  • Is the error because the names of my enums, have "-" in the bank and not in the class

  • 1

    This is because it has records in the size column as NULL, try changing SELECT only for testing purposes: Preparedstatement stmt = this.connection.prepareStatement("select * from contacts Where size != null"); So it returns only record that contains size

  • 1

    That’s right. Of course, I didn’t even notice. In the bank it had a size = null. Otherwise, an ENUM can’t be null?

  • 1

    I updated the answer I hope I helped =). "Something else, can’t an ENUM be null?" Well, in my projects I use Enums in my BD is configured as NOT NULL because I am working with constants. Maybe you can create a default value for if NULL assigns Enum the default value.

  • 1

    It helped yes, I solved it here, I tested it and it’s working.

Show 1 more comment

Browser other questions tagged

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