2016-03-22 45 views
0

私のプロジェクトでは検索が困難です。データをデータベースに挿入することはできません。HTTPステータス500 - 要求の処理に失敗しました。NullException

するコントローラ:

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"); 
} 
} 

ディスパッチャ・サーブレット:

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

誰かが私を助けることができます。私はすでに同様の問題を検索していますが、まだ何がエラーを取得していません。

+0

明らかに、「userDAO」はnullです。 – shmosel

+1

[Null Pointer Exceptionとは何ですか?そしてそれを修正する方法は?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do -i-fix-it) –

答えて

0

あなたはRegisterControllerためか@Autowired @Inject注釈を追加する必要があります:: userDAOフィールド、それは適切に初期化されます。

+0

ありがとうございます。これは私の初めての春の使用 – faridrakh

0

あなたは

private UserDAO userDAO; 

を定義している。そして、あなたがこの時点で

userDAO.create(whatever); 

を呼び出して、値がuserDAOに割り当てられていない、したがって、ヌル。オブジェクトをインスタンス化する必要があります。

関連する問題