Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Pessoal,
Boa Tarde
Estou fazendo um programa simples de cadastro usando jsp. servlet e jdbc...mas quando clico em cadastrar, para jogar pro banco, aparece essa tela de erro:
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
modelo.Conexion.cerrarConexion(Conexion.java:25)
modelo.Usuario.agregarUsuario(Usuario.java:23)
vista.ServletRegistro.doPost(ServletRegistro.java:48)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.12 logs.
Apache Tomcat/7.0.12
Poderiam me ajudar? Já quebrei a cabeça aqui mas nao esta dando certo =/
Seguem os codigos:
INDEX.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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Agenda de Contatos</title>
</head>
<body>
<form action="ServletRegistro" method="post">
<div align="center">
<table>
<tr>
<td colspan="2" align=center><p>AGENDA TELEFONICA</p>
<p>Uezo</p>
<p> </p>
<p>Adicionar Novo Contato:</p></td>
</tr>
<tr>
<td colspan="2"><hr /></td>
</tr>
<tr>
<td bgcolor="#FFFFCC">Nome:</td>
<td bgcolor="#FFFFCC"><input name="nome" type="text" id="nome" /></td>
</tr>
<tr>
<td bgcolor="#FFFFCC">Telefone:</td>
<td bgcolor="#FFFFCC"><input name="telefone" type="text" id="telefone" /></td>
</tr>
<tr>
<td bgcolor="#FFFFCC">Celular:</td>
<td bgcolor="#FFFFCC"><input name="celular" type="text" id="celular" /></td>
</tr>
<tr>
<td colspan="2" align=center bgcolor="#FFFFCC"><input type="submit" name="add" id="add" value="Adicionar Contato"/></td>
</tr>
</table>
</div>
</form>
</body>
</html>package vista;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modelo.Usuario;
import controlador.BeanUsuario;
/**
* Servlet implementation class ServletRegistro
*/
@WebServlet("/ServletRegistro")
public class ServletRegistro extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ServletRegistro() {
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String nome=request.getParameter("nome");
String telefone=request.getParameter("telefone");
String celular=request.getParameter("celular");
if(!nome.equalsIgnoreCase("") && !telefone.equalsIgnoreCase("") && !celular.equalsIgnoreCase("")){
BeanUsuario busuario=new BeanUsuario(nome, telefone, celular);
boolean sw=Usuario.agregarUsuario(busuario);
if(sw){
request.getRequestDispatcher("Mensajem.jsp").forward(request, response);
}else{
PrintWriter out=response.getWriter();
out.println("Erro");
}
}
}
}
BeanUsuario
package controlador;
public class BeanUsuario {
private int id;
private String nome;
private String telefone;
private String celular;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getTelefone() {
return telefone;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
public String getCelular() {
return celular;
}
public void setCelular(String celular) {
this.celular = celular;
}
public BeanUsuario(String nome, String telefone,
String celular) {
super();
//this.id = id;
this.nome = nome;
this.telefone = telefone;
this.celular = celular;
}
}
Conexion
package modelo;
import java.sql.*;
public class Conexion {
private Connection con = null;
public Conexion() {
try {
Class.forName("org.postgresql.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://localhost:5432/agenda", "postgres","adm");
} catch (InstantiationException | IllegalAccessException
| ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Connection getConexion(){
return con;
}
public void cerrarConexion(){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} Usuario
package modelo;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import controlador.BeanUsuario;
public class Usuario {
public static boolean agregarUsuario(BeanUsuario usuario){
boolean agregado=false;
try {
Conexion c=new Conexion();
Connection con=c.getConexion();
if(con!=null){
Statement st;
st = con.createStatement();
st.executeUpdate("INSERT INTO contato(`nome`,`telefone`,`celular`) VALUES ('"+usuario.getNome()+"','"+usuario.getTelefone()+"',"+usuario.getCelular()+"')");
agregado=true;
st.close();
}
c.cerrarConexion();
} catch (SQLException e) {
agregado=false;
e.printStackTrace();
}
return agregado;
}
}
Obrigada
Carregando comentários...