jsf托管bean中的Erreur java.lang.NullPointerException

时间:2014-04-25 21:54:04

标签: spring jsf

你好我已经开始了一个spring jsf项目,当我运行第一页jsf时我发现了这个错误:

java.lang.NullPointerException
com.project.formation.bean.StudentBean.addStudent(StudentBean.java:31)
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)
org.apache.el.parser.AstValue.invoke(AstValue.java:278)
org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:273)
com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
javax.faces.component.UICommand.broadcast(UICommand.java:315)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
你可以帮助我,谢谢你!

问题出在jsf托管bean中,完全在addStudent()方法中:

public class StudentBean implements Serializable {
    @Autowired
    private  StudentService studentService;
    private Student student;
    public StudentBean() {
        student = new Student();
    }

    public void addStudent() {
        studentService.add(student);//line 31
        student=new Student();

    }

    public List<Student> getStudentList() {
        return studentService.findAll();
    }
    public Student getStudent() {
        return student;
    }
    public void setStudent(Student student) {
        this.student = student;
    }
}

我的Dao bean代码是:

public class StudentDaoImpl implements StudentDao {

    @PersistenceContext
    private EntityManager em;

    @Override
    public void add(Student student) {
        em.persist(student);

    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Student> findAll() {

        Query query=em.createQuery("select s from Student s");  
        return query.getResultList();
    }

}

和完整的spring xml文件配置:applicationContext.xml是:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">


<!-- bean of data source -->
    <bean id="datasource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/Student"></property>
        <property name="username" value="root"></property>
        <property name="password" value=""></property>
    </bean>

    <!-- bean of persistence manager -->
    <bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
        <property name="defaultDataSource" ref="datasource"></property>
        <property name="persistenceXmlLocations">
            <list>
                <value>classpath*:META-INF/persistence.xml</value>
            </list>
        </property>
    </bean>

    <!-- bean definition entityManagerFactory -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitManager" ref="persistenceUnitManager"></property>
        <property name="persistenceUnitName" value="UP_STUDENT"></property>
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"></property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>
    <context:annotation-config></context:annotation-config>
    <bean id="studentDao" class="com.project.formation.dao.StudentDaoImpl"/>
    <bean id="studentService" class="com.project.formation.service.StudentServiceImpl"/>
</beans>

2 个答案:

答案 0 :(得分:0)

我怀疑你的StudentService没有自动装配并且为null,请检查以确保它是使用@Service或@component进行注释,以便知道弹出上下文。

如果您的应用需要bean才能运行,通常应该标记为required = true。

答案 1 :(得分:0)

您在StudenBean中没有为学生服务设置的getter和setter