以下是我正在做的事情的简要说明。
应用程序在JSP中具有表单元素,在按下提交后重定向到Controller类。控制器调用服务层,服务层又调用DAO,最后将记录添加到数据库。我遇到的问题是,在Controller类中,我可以接收请求参数(POST)但问题是在调用服务层的addStudent()
时存在问题。问题在于对象上的Null Pointer Exception。你们可以查看代码并向我提供建议吗?
这是完整的Stacktrace,通过指向对象抛出错误,i。nullPointerException
。
INFO: Server startup in 4385 ms
Apr 28, 2014 11:45:38 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [springDispatcher] in context with path [/StudentManagementSystem] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at com.vastika.controllers.SpringController.addStudent(SpringController.java:42)
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 org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2441)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2430)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
以下是代码
SpringController类
package com.vastika.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.vastika.student.model.StudentModel;
import com.vastika.student.services.StudentServiceImpl;
@Controller
@RequestMapping("/students/*")
public class SpringController {
private StudentServiceImpl studentService;
public void setStudentService(StudentServiceImpl studentService) {
this.studentService = studentService;
}
@RequestMapping(value = "/students/addstud", method = RequestMethod.POST)
public String addStudent(@RequestParam(value = "name") String name,
@RequestParam(value = "age") String age,
@RequestParam(value = "address") String address,
@RequestParam(value = "email") String email) {
StudentModel student = new StudentModel();
student.setName(name);
student.setAge(age);
student.setAddress(address);
student.setEmail(email);
if (studentService.addStudentService(student)) {
return "Student is added to the database";
}
return "Student is not added to the database";
}
}
StudentServiceImpl Class
package com.vastika.student.services;
import java.util.List;
import com.vastika.student.dao.StudentDaoImpl;
import com.vastika.student.model.StudentModel;
public class StudentServiceImpl implements IStudentService {
private StudentDaoImpl studentDao;
public void setStudentDao(StudentDaoImpl studentDao) {
this.studentDao = studentDao;
}
@Override
public boolean addStudentService(StudentModel student) {
if(studentDao.addStudentDao(student)){
return true;
}
return false;
}
@Override
public boolean delStudentService(int id) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean updateStudentService(StudentModel student) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean getStudentByIdService(int id) {
// TODO Auto-generated method stub
return false;
}
@Override
public List<StudentModel> getAllStudentsService() {
// TODO Auto-generated method stub
return null;
}
}
模特课程:
package com.vastika.student.model;
public class StudentModel {
private int studentId;
private String name;
private String age;
private String address;
private String email;
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
spring.xml文件
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="student" class="com.vastika.student.model.StudentModel"/>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id="studentService" class="com.vastika.student.services.StudentServiceImpl">
<property name="studentDao" ref="studentDao"></property>
</bean>
<bean id="studentDao" class="com.vastika.student.dao.StudentDaoImpl"/>
<bean id="studentController" class="com.vastika.controllers.SpringController">
<property name="studentService" ref="studentService"></property>
</bean>
</beans>
springdispatcher-servlet.xml文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.vastika.controllers" />
<!-- <bean id="studentService" class="com.vastika.student.services.StudentServiceImpl">
<property name="studentDao" ref="studentDao"></property>
</bean>
<bean id="studentDao" class="com.vastika.student.dao.StudentDaoImpl"/>
<bean id="studentModel" class="com.vastika.student.model.StudentModel"/> -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
web.xml文件
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Archetype Created Web Application</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springDispatcher-servlet.xml;classpath:spring.xml</param-value>
</context-param>
<servlet>
<servlet-name>springDispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
-->
</web-app>
index.jsp文件
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Archetype Created Web Application</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springDispatcher-servlet.xml;classpath:spring.xml</param-value>
</context-param>
<servlet>
<servlet-name>springDispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
-->
</web-app>
答案 0 :(得分:0)
检查部署描述符是否 3.1 ,我认为你有 2.4版本作为部署描述符,这就是为什么Apache Tomcat无法运行。 然后检查是否放置了 web.xml 已部署的资源/ WEB-INF / web.xml