alteração - servlet e Jsp

3 respostas
Luiza_Taques

o código que fiz está acusando esse erro quando compilo:


type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.NullPointerException

persistencia.UsuarioBD.alteraUsuario(UsuarioBD.java:219)

controller.AlteraUsuarioServlet.doPost(AlteraUsuarioServlet.java:67)

javax.servlet.http.HttpServlet.service(HttpServlet.java:710)

javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.14 logs.

vou mandar parte do código:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

HttpSession session = request.getSession(); //obtenção do objeto de sessão
    UsuarioBean u = null;
    int nCpf = 0;
    String strAcao = request.getParameter("acao");
    if (strAcao.equalsIgnoreCase("consultar")) {
        
        try {
             nCpf = new Integer(request.getParameter("cpf")).intValue();
        }
        catch(Exception e) {
            
        }
        
        u = UsuarioBean.getUsuarioByCPF(nCpf);
        
         String redirectURL = "alteraUsuario.jsp?acao=consulta&nome=" + u.getNome();
         response.sendRedirect(redirectURL);
    
    }
    else {
        boolean bErro;
        bErro = UsuarioBD.alteraUsuario(u);

        String verificaTipoLogin = "";
        verificaTipoLogin = UsuarioBD.TipoUsuario(u.getLogin());
        System.out.println(verificaTipoLogin);

        RequestDispatcher rd;
        System.out.println(bErro);

        if (bErro == true) //erro no UPDATE
        {
            session.setAttribute("usuario", u);
            rd = request.getRequestDispatcher("alteraUsuario.jsp");
            rd.forward(request, response);
        }
        else
        {
            if (session.getAttribute("adm") == null)
            {
                response.sendRedirect("consultaUsuario.jsp");
            }
        }
    }
}

}

pelo que percebi ele não está entrado no If, está passando direto.

O código do usuarioBD que está acusando erro é esse:

public static boolean alteraUsuario (UsuarioBean u) throws IOException

{

boolean bErro           = false;

String strNome          = u.getNome();

int intRG               = u.getRG();

int intCpf              = u.getCpf();

int intfone             = u.getFone();

String strEmail         = u.getEmail();

String strLogin         = u.getLogin();

String strSenha         = u.getSenha();

String strTipoUsuario   = u.getTipoUsuario();

String sql              = null;

Boolean strLogado       = u.getLogado();
try
{
    UsuarioBean uBD = buscalogin (u,false);
    int nCpfBD = 0;
    if (uBD != null)
        nCpfBD = uBD.getCpf();
    if (nCpfBD != 0 && (nCpfBD != intCpf)) // já existe
    {
        System.out.println("Usuario já existe, tente outro login");
        bErro = true;
    }
    else
    {
        Connection conn = Conexao.getConnection(); //obtendo conexão
        String str = "usuario";
        sql = "UPDATE INTO usuario(nome, RG, cpf, fone, email, login, senha, tipoUsuario) VALUES ('" + strNome + "', " + intRG + ", " + intCpf + ", " + intfone + ", '" + strEmail + "', '" + strLogin + "', '" + strSenha + "', '" + strTipoUsuario + "')";    
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, strNome);
        ps.setInt(2, intRG);            
        ps.setInt(3, intCpf);
        ps.setInt(4, intfone);            
        ps.setString(5, strEmail);            
        ps.setString(6, strLogin);
        ps.setString(7, strSenha);
                    
        System.out.println(sql);
        
        int i = ps.executeUpdate();
        if (i != 1){
            bErro = true;
            System.out.println ("Erro UPDATE");
        }
        ps.close();
    }
}
catch (SQLException e){
    bErro = true;
    System.out.println ("exceção SQL");
    e.printStackTrace();
}        
catch (Exception e){
    bErro = true;
    System.out.println("exceção");
}
return bErro;

}

public static UsuarioBean obterUsuarioPorCPF(int cpf) {

String strSQL = "";
Statement stm;

UsuarioBean u = new UsuarioBean();
int intCpf = u.getCpf();

try
{
    Connection conn = Conexao.getConnection();
    stm = conn.createStatement();
    strSQL = "SELECT cpf from usuario WHERE cpf = '" + intCpf + "' ";
    ResultSet rs = stm.executeQuery(strSQL);
    
    if (rs.next())
    {
        u.setCpf(intCpf);
    }
    rs.close();
    stm.close();
}
catch (Exception e)
{
    System.out.println("Erro de conexao: " + e.getMessage());
}

return u;

}

e a página Jsp é a seguinte:
<%@page contentType=“text/html” pageEncoding=“UTF-8”%>

ALTERA USUÁRIO

SISTEMA EXAR
<form name="consulta" action="AlteraUsuarioServlet" method="POST" >

CPF



<%

String strAcao = request.getParameter(acao);

String strNome = “”;

int intRg = 0;

int intCpf = 0;

int intFone = 0;

String strEmail = “”;

String strLogin = “”;

String strSenha = “”;

if (strAcao!=null && strAcao.equals(consulta)) {

strNome = request.getParameter(nome);

Integer.parseInt(request.getParameter(rg));

Integer.parseInt(request.getParameter(cpf));

Integer.parseInt(request.getParameter(fone));

strNome = request.getParameter(email);

strNome = request.getParameter(login);

strNome = request.getParameter(senha);

}

%>

Nome 




RG 




CPF 




Telefone 




E-mail 




Login 




Senha

Será que alguém pode me ajudar, me dizendo o que eu fiz de errado

3 Respostas

Pedrosa

O valor esta chegando null nessa linha (UsuarioBD.java:219), e de uma estudada em MVC, vc esta programando estilo ASP.

Luiza_Taques

eu havia percebido que o valor está chegando nulo, porque ele não está parando no if e aí ele está recebendo o u do UsuarioBean nulo, o problema é que eu não estou sabendo como resolver esse problema.

Pedrosa

Você esta usando alguma ferramenta para desenvolver estilo Eclipse ou Netbeans?, veja o que esta acontecendo usando o Debug.

Criado 19 de junho de 2008
Ultima resposta 19 de jun. de 2008
Respostas 3
Participantes 2