0
I’m having trouble trying to list the data.
Exception in thread "main" java.lang.Nullpointerexception at modelo.Dao.getLista(Dao.java:111) at principal.Main.listarViagem(Main.java:161) at principal.Main.menu(Main.java:58) at principal.Main.main(Main.java:39) ()
My codes:
Class
public List<Bean> getLista() {
try {
List<Bean> viagens = new ArrayList<Bean>();
PreparedStatement stmt = ConexaoMySQL.
prepareStatement("select * from viagem");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
// criando o objeto viagem
Bean viagem = new Bean();
viagem.setIdViagem(rs.getInt("idViagem"));
Calendar dataInicio = Calendar.getInstance();
dataInicio.setTime(rs.getDate("dataInicio"));
viagem.setDataInicio(dataInicio);
Calendar dataF = Calendar.getInstance();
dataF.setTime(rs.getDate("dataFim"));
viagem.setDataFim(dataF);
viagem.setCidade(rs.getString("cidade"));
viagem.setUf(rs.getString("uf"));
viagem.setValorDiaria(rs.getDouble("valorDiaria"));
viagem.setColaborador(rs.getString("colaborador"));
viagem.setCliente(rs.getString("cliente"));
// adicionando o objeto à lista
viagens.add(viagem);
}
rs.close();
stmt.close();
return viagens;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
Main class
private static void listarViagem() {
Dao dao = new Dao();
List<Bean> viagens = dao.getLista();
for (Bean contato : viagens) {
System.out.println("IdViagem: " + contato.getIdViagem());
System.out.println("tipo: " + contato.getTipo());
System.out.println("Data de inicio: " +
contato.getDataInicio().getTime() + "\n");
System.out.println("Data de fim: " +
contato.getDataFim().getTime() + "\n");
System.out.println("cidade: " + contato.getCidade());
System.out.println("uf: " + contato.getUf());
System.out.println("valor da diaria: " + contato.getValorDiaria());
System.out.println("colaborador: " + contato.getColaborador());
System.out.println("cliente: " + contato.getCliente());
}
}
Is there a simpler way to list the data using jdbc in java ? What’s the simplest way to do this?
Is making a mistake?
– DiegoAugusto
for your code in main and DAO to become simpler, you can create a Bean constructor by passing the resultset as parameter to set the attributes, and you can also override the toString method, that your for would be only System.out.println(contact)
– Pedro Laini
By the console is giving an error in the getLista method.
– Marcelo T. Cortes
Post your Stacktrace @Jarwin
– Wellington Avelino
Exception in thread "main" java.lang.Nullpointerexception at modelo.Dao.getLista(Dao.java:111) at principal.Main.listarViagem(Main.java:161) at principal.Main.menu(Main.java:58) at principal.Main.main(Main.java:39)
– Marcelo T. Cortes
I just wanted a List method that listed my data to complete my CRUD '-'
– Marcelo T. Cortes
And which line would be 111 from the DAO?
– Maicon Carraro
Resultset rs = stmt.executeQuery();
– Marcelo T. Cortes
how you are implementing Conexaomysql?
– Maicon Carraro
http://pastebin.com/eVsmD2JR
– Marcelo T. Cortes
@Jarwin your way out
prepareStatement("select * from viagem");
returnsnull
so there is exception when trying to executestmt.executeQuery();
– Luídne
The compiler tells me to create a prepareStatement method in my mysql connection class, there is another way to do my dao method ?
– Marcelo T. Cortes