HTTP状态500 - 请求处理失败NullException

时间:2016-03-22 01:02:51

标签: java mysql spring

我的项目中遇到了很难的搜索错误。它不能将数据插入数据库。

我的控制员:

package com.recome.controller;
import com.recome.dao.UserDAO;
import com.recome.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class RegisterController {
    private UserDAO userDAO;

@RequestMapping(value = "/ShowRegisterForm", method = RequestMethod.GET)
public ModelAndView newContact(ModelAndView model) {
    User newUser = new User();
    model.addObject("User", newUser);
    model.setViewName("registerForm");
    return model;
}

@RequestMapping(value = "/saveUser", method = RequestMethod.POST)
public ModelAndView saveUser(@ModelAttribute User user) {
    userDAO.create(user); **//the error point**
    return new ModelAndView("redirect:index.jsp");
}
}

dispatcher-servlet:

<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">

   <bean id="dataSource" 
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
      <property name="url" value="jdbc:mysql://localhost:3306/recome"/>
      <property name="username" value="root"/>
      <property name="password" value=""/>
   </bean>

   <bean id="UserDAOImpl" class="com.recome.dao.UserDAOImpl">
      <property name="dataSource"  ref="dataSource" />    
   </bean>

   <context:component-scan base-package="com.recome.controller" />

   <bean
       class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="prefix">
           <value>/WEB-INF/views/</value>
       </property>
       <property name="suffix">
            <value>.jsp</value>
       </property>
   </bean>
</beans>

用户界面:

package com.recome.dao;

import com.recome.model.User;
import java.util.List;
import javax.sql.DataSource;

public interface UserDAO {

    public void setDataSource(DataSource ds);

    public void create(User user);

    public void update(String email,String password,String fullname,Integer id);

    public User get(Integer id);

    public void delete(Integer id);

    public List<User> list();
}

用户实施:

package com.recome.dao;

import com.recome.model.User;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

public class UserDAOImpl implements UserDAO{

    private JdbcTemplate jdbcTemplate;
    private DataSource dataSource;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }     
    public void create(User user) {
        String query = "insert into user(email,password,fullname) "
                + "values(?,?,?)";
        jdbcTemplate.update(query,user.getEmail(),user.getPassword(),user.getFullname());
    }

    public void update(String email, String password, String fullname, Integer id) {
        String query = "update user set email = ?"+",password = ?"+",fullname = ? where id = ?";
        jdbcTemplate.update(query,email,password,fullname,id);
    }

    public User get(Integer id) {
        return null;
    }

    public void delete(Integer id) {
        String query = "delete from user where id = ?";
        jdbcTemplate.update(query,id);
    }

    public List<User> list() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }    
}

错误:

org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [dispatcher] in context with path [/Recome] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
 java.lang.NullPointerException
    at com.recome.controller.RegisterController.saveUser(RegisterController.java:34)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    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:945)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
    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:648)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2500)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2489)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
有人可以帮助我。我已经搜索了类似的问题,但仍然没有得到错误。

2 个答案:

答案 0 :(得分:0)

您应该为RegisterController :: userDAO字段添加注释 @Inject @Autowired ,然后它将被正确初始化。

答案 1 :(得分:0)

您已定义

private UserDAO userDAO;

然后你打电话

userDAO.create(whatever);

此时,没有为userDAO分配任何值,因此 null 。您需要实例化该对象。