jsf托管bean中出现错误java.lang.NullPointerException



你好,我在运行第一页jsf时启动了一个spring-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;
    }
}

我的刀豆代码是:

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>

我怀疑您的StudentService不是自动连接的,并且为null,请检查以确保它用@Service或@component进行了注释,以便spring上下文知道它。

如果你的应用程序需要一个bean来运行,通常你应该把它标记为required=true。

您的StudenBean 中没有学生服务的getter和setter

相关内容

  • 没有找到相关文章

最新更新