Spring MVC Hibernate : Message Request processing failed; nested exception is java.lang.NullPointerException

         org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:980)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:859)
javax.servlet.http.HttpServlet.service(HttpServlet.java:655)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844)
javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


 Root Cause
  java.lang.NullPointerException
com.emp.controller.EmployeeController.getEdit(EmployeeController.java:123)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:817)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:731)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:968)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:859)
javax.servlet.http.HttpServlet.service(HttpServlet.java:655)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844)
javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Eployee.java

@Entity
@Table(name="employee")
public class Employee {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="emp_id")

private Integer emp_id;

@Column(name="name")
private String name;

@Column(name="designation")
private String designation;

@Column(name="location")
private String location;

@Column(name="employee_type")
private String employee_type;

@OneToMany(mappedBy="employee",fetch = FetchType.EAGER)
private Set<DailyReport> dailyreport;

public Employee() {}
public Employee(String name, String designation, String location, String employee_type) {
super();
this.name = name;
this.designation = designation;
this.location = location;
this.employee_type = employee_type;
}//getters and setters

Daily Report.java:

@Entity
@Table(name="daily_report")
public class DailyReport  {
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;

 @Column(name="date")
 private Date date;

 @Column(name="task_type")
 private  String task_type;

  @Column(name="description")
  private String description;

   @ManyToOne(fetch = FetchType.EAGER)
   @JoinColumn(name="employee_id")
   private Employee employee;
   public DailyReport() {}
   public DailyReport(Date date, String task_type, String description, Employee employee) {
    super();
    this.date = date;
    this.task_type = task_type;
    this.description = description;
    this.employee = employee;
    }//getters and stters

controller

   @Controller
   public class EmployeeController {
   @Autowired
   private EmployeeService employeeService;

   @Autowired
   private DailyReportService dailyreportService;


   @RequestMapping(value = "/editEmployee", method = RequestMethod.GET)
   public String getEdit(@RequestParam(value="employee.emp_id",required=false) Integer emp_id,  
   Model model) {
   DailyReport dailyreport=new DailyReport();
   Employee employee=   employeeService.getEmployee(dailyreport.getEmployee().getEmp_id());
   model.addAttribute("dailyreportAttribute",employee);
   model.addAttribute("emp_id",emp_id);
    return "addemployee";

dao class:

      @Override
      public void edit(Employee employee) {
      Session sessions = session.getCurrentSession();
    
     Employee existingEmployee = (Employee) sessions.get(Employee.class, 
       employee.getEmp_id());
       existingEmployee.setEmp_id(employee.getEmp_id());
       existingEmployee.setName(employee.getName());
       existingEmployee.setDesignation(employee.getDesignation());
       existingEmployee.setLocation(employee.getLocation());
       existingEmployee.setEmployee_type(employee.getEmployee_type());
       sessions.save(existingEmployee);
        }

service class:

                @Transactional
           public void edit(Employee employee) {
            employeeDao.edit(employee);
    

I have one to many mapping in spring MVC and Hibernate.
I’m getting the error as:”org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException” while doing the edit operation in controller. I’m new to spring. Anyone pls help me to solve this. Thank you

Leave a Comment