Essa é minha classe:
import java.sql.*;
public class AgendaDB {
private String nome = blank;
private String endereco = blank;
private String cidade = blank;
private String telefone = blank;
private String result_busca = blank;
private String result_inserir = 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 con = null;
static Statement stm = null;
static ResultSet rs;
static private String blank = "";
public AgendaDB() {
if (con==null) {
try {
Class.forName("org.postgresql.Driver");
con =
DriverManager.getConnection("jdbc:postgresql://localhost:5435/postgres","postgres","eguru");
} catch (SQLException e) {
System.err.println ("Erro: "+e);
con = 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 getCidade() {
return cidade;
}
public void setCidade(String cidade) {
this.cidade = cidade;
}
public String getEndereco() {
return endereco;
}
public void setEndereco(String endereco) {
this.endereco = endereco;
}
public String getTelefone() {
return telefone;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
public String inserir() {
String result_inserir = FALHA_INSERCAO;
try {
stm = con.createStatement();
stm.execute("INSERT INTO teste(nome,endereco,cidade,telefone) VALUES ('" + nome + "','"+
endereco + "','" + cidade + "','"+ telefone + "')");
stm.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 {
stm = con.createStatement();
rs = stm.executeQuery("SELECT * FROM teste WHERE nome = '" + nome + "'");
if (rs.next()) {
nome = rs.getString(1);
endereco = rs.getString(2);
cidade = rs.getString(3);
telefone = rs.getString(4);
result_busca = BUSCA_VALIDA;
}
else
result_busca = BUSCA_INVALIDA;
rs.close();
stm.close();
} catch (SQLException e) {
System.err.println ("Erro: "+e);
}
return result_busca;
}
}
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<html>
<body>
<f:view>
<h:form>
<center><h2> Inserção </h2></center>
<br>
<h3>Entre com os dados abaixo</h3>
<table>
<tr>
<td>Nome:</td>
<td>
<h:inputText value="#{agenda.nome}"/>
</td>
</tr>
<tr>
<td>Endereço:</td>
<td>
<h:inputText value="#{agenda.endereco}"/>
</td>
</tr>
<tr>
<td>Cidade:</td>
<td>
<h:inputText value="#{agenda.cidade}"/>
</td>
</tr>
<tr>
<td>Telefone:</td>
<td>
<h:inputText value="#{agenda.telefone}"/>
</td>
</tr>
</table>
<p>
<h:commandButton value="Inserir" action="#{agenda.inserir}"/>
</p>
</h:form>
<br>
<h:outputLink value="index.jsf">
<f:verbatim>voltar</f:verbatim>
</h:outputLink>
</f:view>
</body>
</html>
e esse é o erro que dá, que eu não sei como resolver.
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: java.lang.NullPointerException
javax.faces.webapp.FacesServlet.service(FacesServlet.java:321)
root cause
javax.faces.el.EvaluationException: java.lang.NullPointerException
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98)
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
javax.faces.component.UICommand.broadcast(UICommand.java:311)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
root cause
java.lang.NullPointerException
AgendaDB.inserir(AgendaDB.java:61)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.apache.el.parser.AstValue.invoke(AstValue.java:196)
org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
org.apache.jasper.el.JspMethodExpression.invoke(JspMethodExpression.java:68)
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:84)
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
javax.faces.component.UICommand.broadcast(UICommand.java:311)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
note The full stack trace of the root cause is available in the JBoss Web/3.0.0-CR1 logs.
JBoss Web/3.0.0-CR1