Spring CRUD应用程序:Servlet [dispatcherServlet]的Servlet.service():java.lang.NullPointerException]

时间:2018-10-18 14:20:35

标签: spring nullpointerexception crud

从mySQL数据库获取员工数据时遇到问题。根据日志,这是第一个问题:在我的EmployeeServiceImpl.listEmployess方法中为36。

日志:

  

2018-10-18 16:07:42.871错误1504-[nio-8090-exec-4] oaccC [。[。[/]。[dispatcherServlet]:Servlet [dispatcherServlet]的Servlet.service()在路径为[]的上下文中引发异常[请求处理失败;嵌套异常是具有根本原因的java.lang.NullPointerException]

     

java.lang.NullPointerException:空

     
    
      

在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(MethodProxy.java:204)〜[spring-core-5.0.9.RELEASE.jar:5.0.9.RELEASE]       在org.springframework.aop.framework.CglibAopProxy $ CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746)〜[spring-aop-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.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)〜[spring-tx-5.0.9.RELEASE.jar:5.0.9.RELEASE]       在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)〜[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()〜[classes /:na]       在com.project.controller.EmployeeController.listEmployess(EmployeeController.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();

您知道这里可能出什么问题吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

您好,而不是使用setter方法,请在依赖项 private void textBox1_TextChanged(object sender, EventArgs e) { string searchstring = textBox1.Text; try { foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Cells[1].Value.ToString().Contains(searchstring)) { Do something } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } 上使用自动装配注释

喜欢

private EmployeeDAO employeeDAO;