DAO调用出现空指针异常



这与任何对象或值为null无关。这是关于我在应用程序中面临的一些配置问题。因此,它不可能是指定为重复的问题的可能重复 我面临的问题是,当我试图使用JSF、Spring和hibernate制作一个简单的示例时。当我从ExpenseForm类调用hibernateDAO函数时,我得到了一个空指针异常,我在ExpenseForm中读取了jsf表单中的所有变量。下面是我的相关代码文件。我使用的是Spring3.0和JSF2.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>

StackTrace:

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)

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

我在faces-config.xml文件中丢失了这个

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

相关内容

  • 没有找到相关文章

最新更新