У меня проблема с получением данных о сотрудниках из базы данных MySQL.Согласно журналам, в строке есть проблема: 36 в моем методе EmployeeServiceImpl.listEmployess.
Журналы:
2018-10-18 16:07:42.871 ОШИБКА 1504 --- [nio-8090-exec-4] oaccC [. [. [/]. [DispatcherServlet]: Servlet.service () для сервлета [dispatcherServlet] в контексте с исключением пути [] выбросило [Ошибка обработки запроса;вложенное исключение - java.lang.NullPointerException] с основной причиной
java.lang.NullPointerException: null
в com.project.service.EmployeeServiceImpl.listEmployess (EmployeeServiceImpl.Java: 36) ~ [classes /: na] com.project.service.EmployeeServiceImpl $$ FastClassBySpringCGLIB $$ c7d76ecc.invoke () ~ [classes /: na] в org.springframework.cglib.proxy.MethodProxy.invoke метод (java: 204) ~ [spring-core-5.0.9.RELEASE.jar: 5.0.9.RELEASE] в org.springframework.aop.framework.CglibAopProxy $ CglibMethodInvocation.invokeJoinpoint (CglibAopProxy.java: -opop.jop:746)-5.0.9.RELEASE.jar: 5.0.9.RELEASE] в org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (ReflectiveMethodInvocation.java:163) ~ [spring-aop-5.0.9.RELEASE.jar: 5.0.9.RELEASE] в org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction (TransactionAspectSupport.java:294) ~ [spring-tx-5.0.9.RELEASE.jar: 5.0.9.RELEASE] в org.springtaction.rainterceptor.TransactionInterceptor.invoke (TransactionInterceptor.java:98) ~ [spring-tx-5.0.9.RELEASE.jar: 5.0.9.RELEASE] в org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (ReflectiveMethodInvoc: 18)) ~ [spring-aop-5.0.9.RELEASE.jar: 5.0.9.RELEASE] в org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept (CglibAopProxy.java:688) ~ [spring-aop-5.0.9.RELEASE.jar: 5.0.9.RELEASE] в com.project.service.EmployeeServiceImpl $$ EnhancerBySpringCGLIB $$ 1390ca06.listEmployess () ~ [классы /: на] в com.project.controller.EmployeeControless.listEmploy (.Java: 34) ~ [classes /: na] в sun.reflect.NativeMethodAccessorImpl.invoke0 (собственный метод) ~ [na: 1.8.0_161] в sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) [na:1.8.0_161] в sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) ~ [na: 1.8.0_161] в java.lang.reflect.Method.invoke (Method.java:498) ~ [na: 1.8.0_161]
EmployeeServiceImpl:
package com.project.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.project.dao.EmployeeDAO;
import com.project.entity.TEmployee;
@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService {
private EmployeeDAO employeeDAO;
public void setEmployeeDAO(EmployeeDAO employeeDAO) {
this.employeeDAO = employeeDAO;
}
@Override
@Transactional
public void addEmployee(TEmployee p) {
this.employeeDAO.addEmployee(p);
}
@Override
@Transactional
public void updateEmployee(TEmployee p) {
this.employeeDAO.updateEmployee(p);
}
@Override
@Transactional
public List<TEmployee> listEmployess() {
return this.employeeDAO.listEmployess();
}
@Override
@Transactional
public TEmployee getEmployeeById(int employee_id) {
return this.employeeDAO.getEmployeeById(employee_id);
}
@Override
@Transactional
public void removeEmployee(int employee_id) {
this.employeeDAO.removeEmployee(employee_id);
}
}
EmployeeService:
package com.project.service;
import java.util.List;
import org.springframework.context.annotation.ComponentScan;
import com.project.entity.TEmployee;
@ComponentScan(basePackages= {"com.project.*"})
public interface EmployeeService {
public void addEmployee(TEmployee p);
public void updateEmployee(TEmployee p);
public List<TEmployee> listEmployess();
public TEmployee getEmployeeById(int id);
public void removeEmployee(int id);
}
EmployeeDAO:
package com.project.dao;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import com.project.entity.TEmployee;
@ComponentScan(basePackages= {"com.project.*"})
public interface EmployeeDAO {
public void addEmployee(TEmployee p);
public void updateEmployee(TEmployee p);
public List<TEmployee> listEmployess();
public TEmployee getEmployeeById(int employee_id);
public void removeEmployee(int employee_id);
}
EmployeeDAOImpl:
package com.project.dao;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.project.entity.TEmployee;
@Repository
public class EmployeeDAOImpl implements EmployeeDAO {
private static final Logger logger = LoggerFactory.getLogger(EmployeeDAOImpl.class);
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sf){
this.sessionFactory = sf;
}
@Override
public void addEmployee(TEmployee p) {
Session session = this.sessionFactory.getCurrentSession();
session.persist(p);
logger.info("TEmployee saved successfully, TEmployee Details="+p);
}
@Override
public void updateEmployee(TEmployee p) {
Session session = this.sessionFactory.getCurrentSession();
session.update(p);
logger.info("TEmployee updated successfully, TEmployee Details="+p);
}
@SuppressWarnings("unchecked")
@Override
public List<TEmployee> listEmployess() {
Session session = this.sessionFactory.getCurrentSession();
List<TEmployee> EmployessList = session.createQuery("from TEmployee").list();
for(TEmployee p : EmployessList){
logger.info("TEmployee List::"+p);
}
return EmployessList;
}
@Override
public TEmployee getEmployeeById(int employee_id) {
Session session = this.sessionFactory.getCurrentSession();
TEmployee p = (TEmployee) session.load(TEmployee.class, new Integer(employee_id));
logger.info("TEmployee loaded successfully, TEmployee details="+p);
return p;
}
@Override
public void removeEmployee(int employee_id) {
Session session = this.sessionFactory.getCurrentSession();
TEmployee p = (TEmployee) session.load(TEmployee.class, new Integer(employee_id));
if(null != p){
session.delete(p);
}
logger.info("TEmployee deleted successfully, TEmployee details="+p);
}
}
И EmployeeController:
package com.project.controller;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.project.entity.TEmployee;
import com.project.service.EmployeeService;
@ComponentScan(basePackages= {"com.project.*"})
@Controller
public class EmployeeController {
@Resource(name = "employeeService")
private EmployeeService employeeService;
@Autowired(required=true)
@Qualifier(value="employeeService")
public void setEmployeeService(EmployeeService ps){
this.employeeService = ps;
}
@RequestMapping(value = "/employess", method = RequestMethod.GET)
public String listEmployess(Model model) {
model.addAttribute("employee", new TEmployee());
model.addAttribute("listEmployess", this.employeeService.listEmployess());
return "employee";
}
//For add and update person both
@RequestMapping(value= "/employee/add", method = RequestMethod.POST)
public String addEmployee(@ModelAttribute("employee") TEmployee p){
if(p.getEmployeeID() == 0){
//new person, add it
this.employeeService.addEmployee(p);
}else{
//existing person, call update
this.employeeService.updateEmployee(p);
}
return "redirect:/employess";
}
@RequestMapping("/remove/{employee_id}")
public String removeEmployee(@PathVariable("employee_id") int employee_id){
this.employeeService.removeEmployee(employee_id);
return "redirect:/employess";
}
@RequestMapping("/edit/{employee_id}")
public String editEmployee(@PathVariable("employee_id") int employee_id, Model model){
model.addAttribute("employee", this.employeeService.getEmployeeById(employee_id));
model.addAttribute("listEmployess", this.employeeService.listEmployess());
return "employee";
}
}
Итак, согласно журналам существует NPEвозвращаясь с этой строки:
return this.employeeDAO.listEmployess();
Вы хоть представляете, что здесь может быть не так?
Заранее спасибо!