错误: -
HTTP状态500 - 请求处理失败;嵌套异常是java.lang.NullPointerException
type Exception report
message Request processing failed; nested exception is java.lang.NullPointerException
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.NullPointerException
com.service.onlineServicesImpl.savEmployee123(onlineServicesImpl.java:33)
com.controller.OnlineBankingController.createNewAccount(OnlineBankingController.java:38)
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.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.67 logs.
Apache Tomcat/7.0.67
spring-distchper-servlet.xml
<context:component-scan base-package="com.controller" />
<context:annotation-config />
<mvc:annotation-driven/>
<bean id="emp" class="com.model.Employee"/>
<bean id="dmds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>
<property name="username" value="system"></property>
<property name="password" value="Oracle@123"></property>
</bean>
<bean id="mysessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dmds"></property>
<property name="mappingResources">
<list>
<value>hibernate.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle12cDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="mysessionFactory"></property>
</bean>
<bean id="eDAO" class="com.dao.EmployeeDAO">
<property name="template" ref="hibernateTemplate"></property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
型号类: - package com.model;
public class Employee {
private String id;
private String name;
private String salary;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSalary() {
return salary;
}
public void setSalary(String salary) {
this.salary = salary;
}
}
DAO班级: - package com.dao;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
import com.model.Employee;
@Repository
public class EmployeeDAO {
public EmployeeDAO() {
System.out.println("In a EmpoyeeDAO******************************");
}
private HibernateTemplate template;
public void setTemplate(HibernateTemplate template) {
System.out.println("In EmployeeDAO setter");
this.template = template;
}
public void save(Employee e) {
System.out.println("EmployeeDAO Value-------> " + e.getId() + " "
+ e.getName() + " " + e.getSalary());
template.save(e);
}
}
Service Class:-
package com.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dao.EmployeeDAO;
import com.model.Employee;
@Service
public class onlineServicesImpl {
@Autowired
private EmployeeDAO dao;
public void savEmployee123(Employee emp) {
System.out.println("Employee Value-------> " + emp.getId() + " "
+ emp.getName() + " " + emp.getSalary());
dao.save(emp);
}
}
这是我的控制器类: -
@Controller
public class OnlineBankingController {
@Autowired
private Employee emp;
@RequestMapping("accountcreation")
public ModelAndView createNewAccount() {
System.out.println("we are in createNewAccount");
emp.setId("10");
emp.setName("pradeep");
emp.setSalary("500000");
// dao.save(emp);
System.out.println("Done");
onlineServicesImpl obj = new onlineServicesImpl();
obj.savEmployee123(emp);
ModelAndView obj1 = new ModelAndView("newAccountCreation");
obj1.addObject("test", "Testing for spring");
return obj1;
}
}
//请帮忙 有人可以提出问题,为什么我的代码无效。