spring boot mvc dao java.lang.NullPointerException: null
Posted
技术标签:
【中文标题】spring boot mvc dao java.lang.NullPointerException: null【英文标题】: 【发布时间】:2016-08-02 00:28:38 【问题描述】:我有一个错误 java.lang.NullPointerException: null 当我尝试调用控制器类的 dao 实现时会发生这种情况 应用程序.properties
server.port = 8080
spring.mvc.view.prefix: /WEB-INF/jsp
spring.mvc.view.suffix: .jsp
spring.datasource.url=jdbc:mysql://localhost/epam_db
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
登录控制器
package ru.av.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import dao.UserDAO;
@Configuration
@ComponentScan("dao")
@Controller
public class LoginController
@Autowired
UserDAO userDao;
@RequestMapping("/")
public String greeting(@RequestParam(value = "name", required = false, defaultValue = "Developer") String name,
Model model)
model.addAttribute("name", name);
return "greeting";
@RequestMapping("login")
public String login(Model model, @RequestParam(value = "login", required = false) String login,
@RequestParam(value = "password", required = false) String password)
if(userDao.getUserByLoginPaswword(login, password) != null)
return "marketplace";
return "login";
浏览器中的一行 http://localhost:8080/login?login=batman&password=batman 用户DAO
package dao;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import dto.UserDTO;
import mapper.UserMapper;
@Repository
public class UserDAO
JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate()
return jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate)
this.jdbcTemplate = jdbcTemplate;
public UserDTO getUserByLoginPaswword(String login, String password)
String sql = "SELECT* FROM epam_db.users WHERE login=? and password=?";
UserDTO user = jdbcTemplate.queryForObject(sql, new Object[] login, password , new UserMapper());
return user;
用户映射器
package mapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
import dto.UserDTO;
public class UserMapper implements RowMapper<UserDTO>
@Override
public UserDTO mapRow(ResultSet rs, int arg1) throws SQLException
UserDTO user = new UserDTO();
user.setEmail(rs.getString("email"));
user.setGender(rs.getString("gender"));
user.setIdUser(rs.getInt("idusers"));
user.setLogin(rs.getString("login"));
user.setPassword(rs.getString("password"));
user.setPhone(rs.getString("phone"));
user.setName(rs.getString("name"));
user.setSurname(rs.getString("surname"));
return user;
我的 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ru.av</groupId>
<artifactId>springMVC</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>springMVC</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- MySQL database driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
错误的全文
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
[32m :: Spring Boot :: [39m [2m (v1.3.2.RELEASE)[0;39m
[2m2016-04-11 21:25:23.979[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[ main][0;39m [36mru.av.SpringMvcApplication [0;39m [2m:[0;39m Starting SpringMvcApplication on batman-ПК with PID 18148 (started by batman in D:\download\Spring-master (10)\Spring-master\springMVC)
[2m2016-04-11 21:25:23.979[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[ main][0;39m [36mru.av.SpringMvcApplication [0;39m [2m:[0;39m No active profile set, falling back to default profiles: default
[2m2016-04-11 21:25:24.042[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[ main][0;39m [36mationConfigEmbeddedWebApplicationContext[0;39m [2m:[0;39m Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@16267862: startup date [Mon Apr 11 21:25:24 SAMT 2016]; root of context hierarchy
[2m2016-04-11 21:25:24.681[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.f.s.DefaultListableBeanFactory [0;39m [2m:[0;39m Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
[2m2016-04-11 21:25:25.009[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[ main][0;39m [36mtrationDelegate$BeanPostProcessorChecker[0;39m [2m:[0;39m Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$e4f0a10b] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
[2m2016-04-11 21:25:25.378[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.b.c.e.t.TomcatEmbeddedServletContainer[0;39m [2m:[0;39m Tomcat initialized with port(s): 8080 (http)
[2m2016-04-11 21:25:25.389[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.apache.catalina.core.StandardService [0;39m [2m:[0;39m Starting service Tomcat
[2m2016-04-11 21:25:25.390[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[ main][0;39m [36morg.apache.catalina.core.StandardEngine [0;39m [2m:[0;39m Starting Servlet Engine: Apache Tomcat/8.0.30
[2m2016-04-11 21:25:25.718[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36morg.apache.jasper.servlet.TldScanner [0;39m [2m:[0;39m At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
[2m2016-04-11 21:25:25.723[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/] [0;39m [2m:[0;39m Initializing Spring embedded WebApplicationContext
[2m2016-04-11 21:25:25.723[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.s.web.context.ContextLoader [0;39m [2m:[0;39m Root WebApplicationContext: initialization completed in 1697 ms
[2m2016-04-11 21:25:26.076[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.s.b.c.e.ServletRegistrationBean [0;39m [2m:[0;39m Mapping servlet: 'dispatcherServlet' to [/]
[2m2016-04-11 21:25:26.080[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.s.b.c.embedded.FilterRegistrationBean [0;39m [2m:[0;39m Mapping filter: 'characterEncodingFilter' to: [/*]
[2m2016-04-11 21:25:26.080[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.s.b.c.embedded.FilterRegistrationBean [0;39m [2m:[0;39m Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
[2m2016-04-11 21:25:26.081[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.s.b.c.embedded.FilterRegistrationBean [0;39m [2m:[0;39m Mapping filter: 'httpPutFormContentFilter' to: [/*]
[2m2016-04-11 21:25:26.081[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[ost-startStop-1][0;39m [36mo.s.b.c.embedded.FilterRegistrationBean [0;39m [2m:[0;39m Mapping filter: 'requestContextFilter' to: [/*]
[2m2016-04-11 21:25:26.374[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerAdapter[0;39m [2m:[0;39m Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@16267862: startup date [Mon Apr 11 21:25:24 SAMT 2016]; root of context hierarchy
[2m2016-04-11 21:25:26.440[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped "[/]" onto public java.lang.String ru.av.controllers.LoginController.greeting(java.lang.String,org.springframework.ui.Model)
[2m2016-04-11 21:25:26.441[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped "[/login]" onto public java.lang.String ru.av.controllers.LoginController.login(org.springframework.ui.Model,java.lang.String,java.lang.String)
[2m2016-04-11 21:25:26.443[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped "[/error]" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
[2m2016-04-11 21:25:26.444[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped "[/error],produces=[text/html]" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
[2m2016-04-11 21:25:26.468[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.w.s.handler.SimpleUrlHandlerMapping [0;39m [2m:[0;39m Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[2m2016-04-11 21:25:26.469[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.w.s.handler.SimpleUrlHandlerMapping [0;39m [2m:[0;39m Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[2m2016-04-11 21:25:26.515[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.w.s.handler.SimpleUrlHandlerMapping [0;39m [2m:[0;39m Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[2m2016-04-11 21:25:26.690[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.j.e.a.AnnotationMBeanExporter [0;39m [2m:[0;39m Registering beans for JMX exposure on startup
[2m2016-04-11 21:25:26.772[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.b.c.e.t.TomcatEmbeddedServletContainer[0;39m [2m:[0;39m Tomcat started on port(s): 8080 (http)
[2m2016-04-11 21:25:26.777[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[ main][0;39m [36mru.av.SpringMvcApplication [0;39m [2m:[0;39m Started SpringMvcApplication in 3.054 seconds (JVM running for 3.609)
[2m2016-04-11 21:26:14.642[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/] [0;39m [2m:[0;39m Initializing Spring FrameworkServlet 'dispatcherServlet'
[2m2016-04-11 21:26:14.642[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m FrameworkServlet 'dispatcherServlet': initialization started
[2m2016-04-11 21:26:14.654[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m FrameworkServlet 'dispatcherServlet': initialization completed in 12 ms
[2m2016-04-11 21:26:14.700[0;39m [31mERROR[0;39m [35m18148[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.a.c.c.C.[.[.[/].[dispatcherServlet] [0;39m [2m:[0;39m Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
at dao.UserDAO.getUserByLoginPaswword(UserDAO.java:24) ~[classes/:na]
at dao.UserDAO$$FastClassBySpringCGLIB$$7befd4b.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720) ~[spring-aop-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) ~[spring-tx-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655) ~[spring-aop-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at dao.UserDAO$$EnhancerBySpringCGLIB$$91e7ee6e.getUserByLoginPaswword(<generated>) ~[classes/:na]
at ru.av.controllers.LoginController.login(LoginController.java:32) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_77]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_77]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_77]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_77]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:860) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:87) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500) [tomcat-embed-core-8.0.30.jar:8.0.30]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456) [tomcat-embed-core-8.0.30.jar:8.0.30]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_77]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_77]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.0.30.jar:8.0.30]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_77]
[2m2016-04-11 21:37:22.379[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[2)-91.146.39.59][0;39m [36minMXBeanRegistrar$SpringApplicationAdmin[0;39m [2m:[0;39m Application shutdown requested.
[2m2016-04-11 21:37:22.379[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[2)-91.146.39.59][0;39m [36mationConfigEmbeddedWebApplicationContext[0;39m [2m:[0;39m Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@16267862: startup date [Mon Apr 11 21:25:24 SAMT 2016]; root of context hierarchy
[2m2016-04-11 21:37:22.384[0;39m [32m INFO[0;39m [35m18148[0;39m [2m---[0;39m [2m[2)-91.146.39.59][0;39m [36mo.s.j.e.a.AnnotationMBeanExporter [0;39m [2m:[0;39m Unregistering JMX-exposed beans on shutdown
【问题讨论】:
由于您已将username
和password
设为可选,因此最好在将它们传递给getUserByLoginPaswword
之前检查它们的无效性。类似if(username != null && password!= null && userDao.getUserByLoginPaswword(login, password) != null)
.
另外,从LoginController
中删除@Configuration
原型注释
NullpointerException 源于
dao.UserDAO.getUserByLoginPaswword(UserDAO.java:24)
如果我数对了,那就是这一行:
UserDTO user = jdbcTemplate.queryForObject(....
在您的代码中,jdbcTemplate 看起来也为空。 它不是自动接线的。将注释 @Autowired 添加到字段 jdbcTemplate 或 setter 中,它应该可以工作。
如果不在第 24 行添加断点,仔细看看那里有什么意外为空。
【讨论】:
我在jdbcTemplate
上添加了带注释的@Autowired
,现在我有错误'...无法自动装配字段:org.springframework.jdbc.core.JdbcTemplate dao.UserDAO.jdbcTemplate;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency: 预期至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:@org.springframework.beans.factory.annotation.Autowired(required=true)'【参考方案2】:
哦,我很愚蠢......
而是 <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
需要使用<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
然后spring创建dataSource和JDBCTemplate
【讨论】:
【参考方案3】:应通过基于 setter 的注入或基于构造函数的注入来注入数据源。试一试,我希望它会奏效。
@Repository
public class UserDAO
JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate()
return jdbcTemplate;
@Autowired
public void setJdbcTemplate(final DataSource dataSource)
this.jdbcTemplate = new JdbcTemplate(dataSource);
public UserDTO getUserByLoginPaswword(String login, String password)
String sql = "SELECT* FROM epam_db.users WHERE login=? and password=?";
UserDTO user = jdbcTemplate.queryForObject(sql, new Object[] login, password , new UserMapper());
return user;
【讨论】:
以上是关于spring boot mvc dao java.lang.NullPointerException: null的主要内容,如果未能解决你的问题,请参考以下文章
Java学习笔记:Spring Boot与Spring MVC的区别是什么?
Java快速开发平台,基于(Spring Boot、Spring MVC、Apache Shiro、MyBatis、Beetl、Bootstrap、AdminLTE),在线代码生成,包括组织、角色用户
JUnit 在带有 @Autowired 注释的 Spring Boot 中不起作用