0
votes

I'm currently working on an sql Class for JSF website. But I've got this annoying error while trying to access my class :

action="#{sqlBean.createAccount}": Target Unreachable, identifier 'sqlBean' resolved to null

Here is the code :

Java class:

package com.tutorial;

import java.sql.*;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(eager = true)
@RequestScoped

public class SqlBean {

    Connection  connec;
    Statement   flux;
    ResultSet   res;

    SqlBean()
    {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connec = DriverManager.getConnection("jdbc:mysql://localhost/epiMarket", "root", "seN?/g0u");
            flux = connec.createStatement();
        }
        catch(Exception e)
        {
            System.out.print("imposible to connect sql");
        }
    }

    ResultSet command(String cmd)
    {
        try {
            res = flux.executeQuery(cmd);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return res;
    }

    public String   createAccount()
    {
        try {
            flux.executeQuery("insert into epi_user values('', #{registerBean.name}, #{registerBean.surname}, '', #{registerBean.date}, #{registerBean.phone}, #{registerBean.login}, #registerBean.password)");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return ("Register");
    }
} 

here is where i access it :

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">

<ui:composition template="/WEB-INF/templates/BasicTemplate.xhtml">
    <ui:define name="content">
    <div id="formulaire">
    <h:form>
        <h:panelGrid columns="6">
        <h:outputText value="Name"></h:outputText>
        <h:inputText value="#{registerBean.name}"></h:inputText>
        <h:outputText value="Surname"></h:outputText>
        <h:inputText value="#{registerBean.surname}"></h:inputText>
        <h:outputText value="Birth (format : dd/mm/yyyy)"></h:outputText>
        <h:inputText value="#{registerBean.date}"></h:inputText>
        <h:outputText value="Login"></h:outputText>
        <h:inputText value="#{registerBean.login}"></h:inputText>
        <h:outputText value="Password"></h:outputText>
        <h:inputSecret value="#{registerBean.password}"></h:inputSecret>
        <h:outputText value="retype password"></h:outputText>
        <h:inputSecret value="#{registerBean.passwordVerif}"></h:inputSecret>
        <h:commandButton value="Register" action="#{sqlBean.createAccount}"></h:commandButton>
    </h:panelGrid>
    </h:form>
    </div>
    </ui:define>
</ui:composition>
</html>

for me, it's sqlBean which is not instantiate but I don't understand why.

Ok, add a Public before SqlBean() and now have a java.lang.NullPointerException

Here the stacktrace :

exception

javax.servlet.ServletException: java.lang.NullPointerException
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:321)
cause mère

javax.faces.el.EvaluationException: java.lang.NullPointerException
    javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98)
    com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
    javax.faces.component.UICommand.broadcast(UICommand.java:311)
    javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
    javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
    com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
cause mère

java.lang.NullPointerException
    com.tutorial.SqlBean.createAccount(SqlBean.java:44)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:622)
    org.apache.el.parser.AstValue.invoke(AstValue.java:278)
    org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
    com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102)
    javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:84)
    com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
    javax.faces.component.UICommand.broadcast(UICommand.java:311)
    javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
    javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
    com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
1
Have you tried having a public constructor for the bean? - mabi
I've just do it. The error has now change to : Etat HTTP 500 -java.lang.NullPointerException - Thomas Grockowiak
Please edit your post with the stacktrace if you can't figure out where the NullPointerException comes from. Also, what's the purpose of this site? If your answer is anything but "fooling around", please consult a tutorial before continuing. - mabi
the purpose of this site is not to fooling around... It's a project for a market WebSite. - Thomas Grockowiak
flux.executeQuery is line 44? I suggest you step back a bit and take a solid Java tutorial, then work through the tutorial link I posted earlier. - mabi

1 Answers

0
votes

The reason for your Exception is most likely that your

flux = connec.createStatement();

wasn't executed. You'll find out by removing that try-catch block from the class' constructor. Reasons for that I can only speculate on - need to deploy the required mysql/jdbc jars perhaps?