boa tarde galera, recentemente comecei a estudar jsf na faculdade, agora de férias estou exercitando. mas esbarrei em um problema meu jsf não salva no mysql segue o erro [code]exception
javax.servlet.ServletException
javax.faces.webapp.FacesServlet.service(FacesServlet.java:325)
root cause
java.lang.NullPointerException
model.userDao.adiciona(userDao.java:25)
control.UserManagedBean.adiciona(UserManagedBean.java:30)
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)
javax.el.BeanELResolver.invoke(BeanELResolver.java:484)
javax.el.CompositeELResolver.invoke(CompositeELResolver.java:161)
org.apache.el.parser.AstValue.getValue(AstValue.java:173)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
com.sun.faces.facelets.el.ELText$ELTextVariable.writeText(ELText.java:224)
com.sun.faces.facelets.el.ELText$ELTextComposite.writeText(ELText.java:148)
com.sun.faces.facelets.compiler.TextInstruction.write(TextInstruction.java:85)
com.sun.faces.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:82)
com.sun.faces.facelets.compiler.UILeaf.encodeAll(UILeaf.java:183)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1655)
com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:399)
com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)[/code]
segue minhas classes[code]public Connection getConnection() {
try {
/*String driverName = "com.mysql.jdbc.Driver";
Class.forName(driverName);*/
String serverName = "localhost";
String mydatabase = "java";
String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
String username = "root";
String password = "";
Connection connection = DriverManager.getConnection(url, username, password);
System.out.println("sucesso");
return connection;
/*} catch (ClassNotFoundException e) {
System.out.println("O driver expecificado nao foi encontrado.");
return null;*/
} catch (SQLException e) {
System.out.println("Nao foi possivel conectar ao Banco de Dados.");
return null;
}
}
}[/code]
[code]private int id;
private String nome;
private int cpf;
private String email;
private int telefone;
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 int getCpf() {
return cpf;
}
public void setCpf(int cpf) {
this.cpf = cpf;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getTelefone() {
return telefone;
}
public void setTelefone(int telefone) {
this.telefone = telefone;
}
}
[/code]
[code]public class userDao {
private Connection connection;
public userDao() {
try {
this.connection = new ConnectionFactory().getConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
public void adiciona(User user) {
String sql = "INSERT INTO usuario(id,nome,cpf,email,telefone) VALUES(?,?,?,?,?)";
try {
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setInt(1, user.getId());
stmt.setString(2, user.getNome());
stmt.setLong(3, user.getCpf());
stmt.setString(4, user.getEmail());
stmt.setInt(5, user.getTelefone());
stmt.execute();
stmt.close();
} catch (SQLException u) {
throw new RuntimeException(u);
}
}
}
[/code]
[code]@ManagedBean
public class UserManagedBean {
private User user = new User();
private userDao dao = new userDao();
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String cadastra() {
dao.adiciona(user);
return "cadastrado";
}
}[/code]
testei essa connectionFactory separada e está funcionando queria um outro olhar para esse problema.