【发布时间】:2014-02-06 18:59:57
【问题描述】:
我目前正在为 JSF 网站开发一个 sql 类。但是我在尝试访问我的课程时遇到了这个烦人的错误:
action="#{sqlBean.createAccount}": Target Unreachable, identifier 'sqlBean' resolved to null
代码如下:
Java 类:
包com.tutorial; 导入java.sql.*; 导入 javax.faces.bean.ManagedBean; 导入 javax.faces.bean.RequestScoped; @ManagedBean(渴望=真) @RequestScoped 公共类 SqlBean { 连接连接; 语句通量; 结果集资源; SqlBean() { 尝试 { Class.forName("com.mysql.jdbc.Driver"); connec = DriverManager.getConnection("jdbc:mysql://localhost/epiMarket", "root", "seN?/g0u"); 通量 = connec.createStatement(); } 捕获(异常 e) { System.out.print("无法连接sql"); } } 结果集命令(字符串 cmd) { 尝试 { res = 通量.executeQuery(cmd); } 捕捉(SQLException e){ // TODO 自动生成的 catch 块 e.printStackTrace(); } 返回资源; } 公共字符串 createAccount() { 尝试 { Flux.executeQuery("插入 epi_user 值('', #{registerBean.name}, #{registerBean.surname}, '', #{registerBean.date}, #{registerBean.phone}, #{registerBean.login} , #registerBean.password)"); } 捕捉(SQLException e){ // TODO 自动生成的 catch 块 e.printStackTrace(); } 返回(“注册”); } }这是我访问它的地方:
<!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>
对我来说,是 sqlBean 没有实例化,但我不明白为什么。
好的,在SqlBean() 之前添加一个Public,现在有一个java.lang.NullPointerException
这里是堆栈跟踪:
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)
【问题讨论】:
-
您是否尝试过为 bean 使用
public构造函数? -
我刚刚做了。错误现在更改为:
Etat HTTP 500 -java.lang.NullPointerException -
如果您无法确定
NullPointerException的来源,请使用堆栈跟踪编辑您的帖子。另外,这个网站的目的是什么?如果您的回答不是“胡闹”,请在继续之前咨询tutorial。 -
这个网站的目的不是为了鬼混......这是一个市场网站的项目。
-
flux.executeQuery是第 44 行吗?我建议你退后一步,学习一个扎实的 Java 教程,然后通过我之前发布的教程链接进行操作。