Sou iniciante em Java estou tendo esse problema no momento em que tento cadastrar alguns valores.
-----------------------------------------------//-------------------//-----------------------------------------------------------------
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
br.com.atividade1.dao.UsuarioDao.cadastraUsuario(UsuarioDao.java:41)
br.com.atividade1.controller.CadastrarUsuario.processRequest(CadastrarUsuario.java:38)
br.com.atividade1.controller.CadastrarUsuario.doPost(CadastrarUsuario.java:46)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.56 logs.
-----------------------------------------------//-------------------//-----------------------------------------------------------------
Segue o Codigo Abaixo
no meu model os dados são:
private int id;
private int tipo;
private int nivel;
private String matricula;
private String login;
private String senha;
No Banco o id é Chave primária auto Incremento.
-----------------------------------------------//-------------------//-----------------------------------------------------------------
package br.com.atividade1.dao;
import java.sql.Connection;
import java.sql.SQLException;
import br.com.atividade1.connection.factory.Conexao;
import br.com.atividade1.javabean.model.UsuarioBean;
import java.sql.PreparedStatement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class UsuarioDao {
private Connection con;
public UsuarioDao() {
this.con = new Conexao().getConnection();
}
UsuarioBean usuario = new UsuarioBean();
public void cadastraUsuario(UsuarioBean usuario) throws SQLException {
String sql = " insert into cadastro_usuario values(?,?,?,?,?,?)";
PreparedStatement ps = null;
try {
ps = con.prepareStatement(sql);
ps.setInt(1, 0);
ps.setInt(2, usuario.getTipo());
ps.setInt(3, usuario.getNivel());
ps.setString(4, usuario.getMatricula());
ps.setString(5, usuario.getLogin());
ps.setString(6, usuario.getSenha());
ps.execute();
} catch (SQLException ex) {
Logger.getLogger(UsuarioDao.class.getName()).log(Level.SEVERE, null, ex);
} finally {
con.close();
ps.close();
}
}
}
-----------------------------------------------//-------------------//-----------------------------------------------------------------
package br.com.atividade1.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import br.com.atividade1.javabean.model.UsuarioBean;
import br.com.atividade1.dao.UsuarioDao;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class CadastrarUsuario extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException {
String tipo = request.getParameter("tipo");
String nivel = request.getParameter("nivel");
String matricula = request.getParameter("matricula");
String login = request.getParameter("login");
String senha = request.getParameter("senha");
UsuarioBean usuario_bean = new UsuarioBean();
usuario_bean.setTipo(Integer.parseInt(tipo));
usuario_bean.setNivel(Integer.parseInt(nivel));
usuario_bean.setMatricula(matricula);
usuario_bean.setLogin(login);
usuario_bean.setSenha(senha);
UsuarioDao usuario_dao = new UsuarioDao();
usuario_dao.cadastraUsuario(usuario_bean);
request.getRequestDispatcher("testeCadastro.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
request.setAttribute("msg", "Dados Gravados!");
request.getRequestDispatcher("testeCadastro.jsp").forward(request, response);
} catch (SQLException ex) {
Logger.getLogger(CadastrarUsuario.class.getName()).log(Level.SEVERE, null, ex);
request.setAttribute("msg", "Dados Não Gravados!");
request.getRequestDispatcher("testeCadastro.jsp").forward(request, response);
}
}
}
