1

This is my controller class

I have a problem when I persist data in mysql then @autowired field is null. I use spring,jpa,and primefaces

 package com.netizenbd.controller;

    import javax.faces.bean.ManagedBean;
    import org.apache.log4j.Logger;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Controller;
    import com.netizenbd.dao.StudentDao;
    import com.netizenbd.domain.Student;

    @ManagedBean
    @Controller
    @Scope("session")
    public class StudentMB {

    final static Logger logger = Logger.getLogger(StudentMB.class);

    @Autowired
    public StudentDao studentDao;
    private Student student;

    public void saveStudent(){
        try {
            studentDao.persist(student);
        } catch (Exception e) {
            logger.debug("This is debug :" + e);
            logger.error("This is error : " + e);
            logger.fatal("This is fatal : " + e);
            e.printStackTrace();
        }
    }
    public Student getStudent() {
        if(student==null){
            student = new Student();
        }
        return student;
    }
    public void setStudent(Student student) {
        this.student = student;
    }
    }

This is my service class

package com.netizenbd.service;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.netizenbd.dao.StudentDao;
import com.netizenbd.domain.Student;

@Transactional
@Component
public class StudentService extends
EntityService<Student> implements StudentDao{

}

This is my DAO interface

Whis is @autowired in controller

 package com.netizenbd.dao;

    import com.netizenbd.domain.Student;

    public interface StudentDao extends EntityDao<Student>{

    }

This is my applicationContext.xml file

<?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:context="http://www.springframework.org/schema/context"                     
 xmlns:jdbc="http://www.springframework.org/schema/jdbc"
 xmlns:mvc="http://www.springframework.org/schema/mvc"     
 xmlns:tx="http://www.springframework.org/schema/tx"
 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-4.2.xsd   
 http://www.springframework.org/schema/jdbc
 http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd 
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

<!-- Scan for all of Spring components such as Spring Service -->
<context:component-scan base-package="com.netizenbd">
</context:componentscan>  
<context:annotation-config/>
<!-- Import all other context files -->
<import resource="classpath:META-INF/spring/applicationContext-*.xml" />

<!-- MVC annotation Driven -->
<mvc:annotation-driven />
</beans>

The exception

     java.lang.NullPointerException
     at com.netizenbd.controller.StudentMB.saveStudent(StudentMB.java:26)
     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 com.sun.el.parser.AstValue.invoke(AstValue.java:234)
     at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
    javax.faces.component.MethodBindingMethodExpressionAdapter
   .invoke(MethodBindingMethodExpressionAdapter.java:87)
    org.springframework.faces.webflow.FlowActionListener.processAction(FlowActio
    org.springframework.faces.model.SelectionTrackingActionListener.
    processAction(SelectionTrackingActionListener.java:64)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    at  com.sun.faces.lifecycle.InvokeApplicationPhase
    .execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)

I don't know what I'm doing wrong. Please let me know what wrong with this configuration.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Md. Nasir Uddin
  • 151
  • 1
  • 13
  • Do you set student before using save method? – ByeBye Aug 09 '16 at 16:23
  • Spring autowired being null is not a PF related issue. Not even a java-se one. And `@ManagedBean` and `@Controller` on the same class looks weird to me... don't mix jsf managed beans and spring managed ones. Pick one mechanism. http://stackoverflow.com/questions/12847436/null-pointer-when-autowiring-the-bean-into-jsf-managed-bean is the duplicate that proposes it the other way around – Kukeltje Aug 09 '16 at 17:12
  • Possible duplicate of [Spring JSF integration: how to inject a Spring component/service in JSF managed bean?](http://stackoverflow.com/questions/18387993/spring-jsf-integration-how-to-inject-a-spring-component-service-in-jsf-managed) – Kukeltje Aug 09 '16 at 17:16
  • I can success fully solve this problem from your Spring JSF integration: how to inject a Spring component/service in JSF managed bean? link – Md. Nasir Uddin Aug 10 '16 at 10:51

0 Answers0