DAO调用时出现空指针异常

时间:2014-04-25 06:36:19

标签: java spring hibernate jsf jsf-2

这不是关于任何对象或值为null。这是关于我面对应用程序的一些配置问题。因此,它不能成为指定为重复的问题的重复。 我面临的问题是,当我尝试使用JSF,Spring和hibernate做一个简单的例子时。当我从ExpenseForm类进行hibernateDAO函数调用时,我从jsf表单读取所有变量,我得到一个空指针异常。下面是我的相关代码文件。我使用的是Spring 3.0和JSF 2.0配置。

ExpenseBean:

@Entity
@Table(name = "Expenses")
public class ExpenseBean implements Serializable {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private int food;
    private int travel;
    private int living;

    @Id
    @Column(name = "Food", nullable = false)
    public int getFood() {
        return food;
    }

    public void setFood(int food) {
        this.food = food;
    }

    @Column(name = "Travel", nullable = false)
    public int getTravel() {
        return travel;
    }

    public void setTravel(int travel) {
        this.travel = travel;
    }

    @Column(name = "Living", nullable = false)
    public int getLiving() {
        return living;
    }

    public void setLiving(int living) {
        this.living = living;
    }


}

ExpenseForm:

public class ExpenseForm {

    private int food;
    private int travel;
    private int living;

    private ConfigureExpenseDAO configureExpenseDAO;

    public ConfigureExpenseDAO getConfigureExpenseDAO() {
        return configureExpenseDAO;
    }

    public void setConfigureExpenseDAO(ConfigureExpenseDAO configureExpenseDAO) {
        this.configureExpenseDAO = configureExpenseDAO;
    }

    public String addExpense() {

        ExpenseBean expense = new ExpenseBean();
        System.out.println(food);
        expense.setFood(food);
        expense.setLiving(living);
        expense.setTravel(travel);
        System.out.println(expense.getFood() + expense.getLiving());


    /*line 33*/
        configureExpenseDAO.saveExpenseDetails(food, living, travel);
        return "index";

    }

    public int getFood() {
        return food;
    }

    public void setFood(int food) {
        this.food = food;
    }

    public int getTravel() {
        return travel;
    }

    public void setTravel(int travel) {
        this.travel = travel;
    }

    public int getLiving() {
        return living;
    }

    public void setLiving(int living) {
        this.living = living;
    }

}

ConfigureExpenseDAOImpl:

@Repository("configureExpenseDAO")
public class ConfigureExpenseDAOImpl extends HibernateAbstractBase implements ConfigureExpenseDAO{


    @Override
    public void saveExpenseDetails(int food, int living, int travel) {
        // TODO Auto-generated method stub
        System.out.println("DAO");
        ExpenseBean expense=new ExpenseBean();
        expense.setFood(food);
        expense.setLiving(living);
        expense.setTravel(travel);
        HibernateTemplate hTemp = getHibernateTemplate(); 
        hTemp.saveOrUpdate(expense);
    }
}

SessionFactory.xml

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
   <property name="dataSource" ref="dataSource"></property>
    <property name="annotatedClasses">             
        <list>                 
            <value>com.src.jsfsample.model.ExpenseBean</value>

        </list>         
    </property>           
    <property name="hibernateProperties">             
        <props>                 
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
            <prop key="hibernate.show_sql">false</prop>               
            <!-- <prop key="hibernate.current_session_context_class">thread</prop> -->             
        </props>         
    </property>     
 </bean>

的applicationContext.xml

 <context:component-scan base-package="com.src.jsfsample"/>
    <import resource="/SpringBeans/DataSource.xml"/>
    <import resource="/SpringBeans/SessionFactory.xml"/>

faces-config.xml中

<managed-bean>
    <managed-bean-name>expenseForm</managed-bean-name>
    <managed-bean-class>com.src.jsfsample.managedbean.ExpenseForm</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
    <managed-property>
        <property-name>configureExpenseDAO</property-name>
        <value>#{configureExpenseDAO}</value>
    </managed-property>
    </managed-bean>

堆栈跟踪:

20
40
SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/JSFExample] threw exception [javax.el.ELException: java.lang.NullPointerException] with root cause
java.lang.NullPointerException
    at com.src.jsfsample.managedbean.ExpenseForm.addExpense(ExpenseForm.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:277)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:273)
    at org.apache.myfaces.view.facelets.el.ContextAwareTagMethodExpression.invoke(ContextAwareTagMethodExpression.java:96)
    at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:74)
    at javax.faces.component.UICommand.broadcast(UICommand.java:120)
    at javax.faces.component.UIViewRoot._broadcastAll(UIViewRoot.java:1172)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:365)
    at javax.faces.component.UIViewRoot._process(UIViewRoot.java:1656)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:862)
    at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:42)
    at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:196)
    at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:143)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:196)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:357)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

2 个答案:

答案 0 :(得分:0)

模型ExpenseBean : food,travel,living的属性值可能为null。你应该为它们设定价值!

答案 1 :(得分:0)

我在faces-config.xml文件中错过了这个

<application>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    </application>