Pessoal recentemente comecei a estuda JEE e jsf
fiz um projetinho pra brinca ae veio varias duvidas fiz uma classe de persistencia DAOCliente q persiste e nela ela espera um objeto TOCliente aonde ta os sets e gets
Fiz um controler que que pega o contexto que chama a classe dao e passa o ojeto so que qndo vo usar o objeto vindo no contexto ele da o erro de nullpointer exeption
vo posta as classes
package DAO;
import java.sql.ResultSet;
import java.sql.SQLException;
import util.Conexao;
import TO.TOCliente;
import com.mysql.jdbc.PreparedStatement;
public class DAOCliente {
private ResultSet res;
private PreparedStatement pre;
public void addCliente(TOCliente toCliente){
try{
Conexao con = new Conexao();
pre = (PreparedStatement) con.getConnection().prepareStatement("INSERT INTO cliente(nome, end)" +
"VALUES(?,?);");
pre.setString(1, toCliente.getNome());
pre.setString(2, toCliente.getEnd());
System.out.println(toCliente.getNome());
System.out.println(toCliente.getEnd());
pre.execute();
}catch (SQLException e) {
e.printStackTrace();
}
}
}
package Controler;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import TO.TOCliente;
import util.*;
import DAO.*;
public class CONTROLECadastro {
public String novoCadastro(){
TOCliente toCliente = (TOCliente)FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("toCliente");
DAOCliente daoCliente = new DAOCliente();
daoCliente.addCliente(toCliente);
return null;
}
}
package TO;
public class TOCliente {
private String nome;
private String end;
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getEnd() {
return end;
}
public void setEnd(String end) {
this.end = end;
}
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!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>Insert title here</title>
</head>
<body>
<f:view>
<h:form id="forumlario">
Digite seu nome aqui:<h:inputText id="nome" value="#{TOCliente.nome}"></h:inputText><br>
Digite seu endereço aqui:<h:inputText id="end" value="#{TOCliente.end}"></h:inputText><br>
<h:commandButton value="Enviar" id="submit" action="#{ControleCadastro.novoCadastro}"> </h:commandButton>
</h:form>
</f:view>
</body>
</html>