Galera boa tarde, tudo bem?
Alguém sabe porque estou recebendo valor null, neste form? Quando eu
coloco o nome do cara pra ser buscado no campo text, ele me retorna
null…
Segue meu codigo fonte…
AgendaDB
import java.sql.*;
public class AgendaDB {
private String nome = blank;
private String email = blank;
public static final String BUSCA_INVALIDA = "failure";
public static final String BUSCA_VALIDA = "success";
public static final String SUCESSO_INSERCAO = "success";
public static final String FALHA_INSERCAO = "failure";
static Connection conn = null;
static Statement stmt = null;
static ResultSet rs;
static private String blank = "";
public AgendaDB() {
if (conn == null) {
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/
thiago","root","root");
} catch (SQLException e) {
System.err.println("Erro: " + e);
conn = null;
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFound...");
e.printStackTrace();
}
}
}
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 inserir() {
String result_inserir = FALHA_INSERCAO;
try {
stmt = conn.createStatement();
stmt.execute("insert into contatos(nome, email) values ('" + nome
+ "','" + email + "')");
stmt.close();
result_inserir = SUCESSO_INSERCAO;
} catch (SQLException e) {
System.err.println("Erro: " + e);
result_inserir = FALHA_INSERCAO;
}
return result_inserir;
}
public String buscar() throws SQLException {
String result_busca = BUSCA_INVALIDA;
try {
PreparedStatement stmt = conn
.prepareStatement("select * from contatos where nome=?");
stmt.setString(1, getNome());
rs = stmt.executeQuery();
if (rs.next()) {
this.setNome(rs.getString("nome"));
this.setEmail(rs.getString("email"));
result_busca = BUSCA_VALIDA;
} else {
result_busca = BUSCA_INVALIDA;
rs.close();
stmt.close();
}
} catch (SQLException e) {
System.err.println("Erro: " + e);
}
return result_busca;
}
}
busca.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://
www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<title>Busca</title>
</head>
<body>
<f:view>
<h:form>
<center><h2>Busca</h2></center>
<br>
Digite o nome:
<h:inputText id="nome" value="#{agenda.nome}"/>
<h:commandButton value="ok" action="#{agenda.buscar}"/>
</h:form>
<br>
<h:outputLink value="index.jsf">
<f:verbatim>Voltar</f:verbatim>
</h:outputLink>
</f:view>
</body>
</html>
Erro
javax.servlet.ServletException: #{agenda.buscar}:
java.lang.NullPointerException
javax.faces.webapp.FacesServlet.service(FacesServlet.java:256)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:
96)
root cause
javax.faces.FacesException: #{agenda.buscar}:
java.lang.NullPointerException
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:
107)
javax.faces.component.UICommand.broadcast(UICommand.java:383)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:447)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:
752)
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:
97)
com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:
96)