【问题标题】:Erreur java.lang.NullPointerException in jsf managed beanjsf托管bean中的错误java.lang.NullPointerException
【发布时间】:2014-04-25 21:54:03
【问题描述】:

您好,我已经启动了一个 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;
    }
}

我的道豆代码是:

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>

【问题讨论】:

  • 请显示一些代码。
  • 公共类 StudentBean 实现可序列化 { @Autowired private StudentService studentService;私人学生学生;公共StudentBean(){学生=新学生(); } 公共无效 addStudent() { studentService.add(student);学生=新学生(); } public List getStudentList() { return studentService.findAll(); } 公共学生 getStudent() { 返回学生; } public void setStudent(Student student) { this.student = student; } }
  • 我将这个注解用于我的管理 bean:@ManagedBean(name="studentBean")@Scope@Component
  • 请编辑您的问题,在此处添加代码并标记您的 StudentBean 的第 31 行。

标签: spring jsf


【解决方案1】:

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

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

【讨论】:

  • 我使用 @service 注释,但是当我在 applicationContext.xml 文件中配置我的 bean 服务时: 问题仍未解决
  • 好的,所以你要么使用 xml 要么使用注解,但通常不能同时使用两者,要么使用自动扫描并给它一个基本路径,因此 Spring 可以扫描你的包中的组件、服务和存储库。或者,如果您在 xml 文件中进行配置,请确保您的代码中没有任何地方正在执行 service = new Service()。在 Spring 3 中,任何通过 new 创建的实例都不为 Spring 上下文所知。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2014-03-19
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    • 2012-10-13
    • 2011-07-10
    相关资源
    最近更新 更多