作了個簡單的springboot項目,一開始全部運行正常,以後寫了一天的代碼,再運行相同的代碼,就一直報如下異常:html
This application has no explicit mapping for /error, so you are seeing this as a fallback.java
java.lang.NullPointerException: null
at org.springframework.boot.web.servlet.view.velocity.EmbeddedVelocityToolboxView.setContextToolbox(EmbeddedVelocityToolboxView.java:65) ~[spring-boot-1.3.8.RELEASE.jar:1.3.8.RELEASE]
at org.springframework.boot.web.servlet.view.velocity.EmbeddedVelocityToolboxView.createVelocityContext(EmbeddedVelocityToolboxView.java:54) ~[spring-boot-1.3.8.RELEASE.jar:1.3.8.RELEASE]
at org.springframework.web.servlet.view.velocity.VelocityView.renderMergedTemplateModel(VelocityView.java:288) ~[spring-webmvc-4.2.8.RELEASE.jar:4.2.8.RELEASE]
at org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:167) ~[spring-webmvc-4.2.8.RELEASE.jar:4.2.8.RELEASE]
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303) ~[spring-webmvc-4.2.8.RELEASE.jar:4.2.8.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1246) ~[spring-webmvc-4.2.8.RELEASE.jar:4.2.8.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1029) ~[spring-webmvc-4.2.8.RELEASE.jar:4.2.8.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:973) ~[spring-webmvc-4.2.8.RELEASE.jar:4.2.8.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895) ~[spring-webmvc-4.2.8.RELEASE.jar:4.2.8.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967) ~[spring-webmvc-4.2.8.RELEASE.jar:4.2.8.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858) ~[spring-webmvc-4.2.8.RELEASE.jar:4.2.8.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) ~[tomcat-embed-core-8.0.37.jar:8.0.37]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843) ~[spring-webmvc-4.2.8.RELEASE.jar:4.2.8.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) ~[tomcat-embed-core-8.0.37.jar:8.0.37]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292) ~[tomcat-embed-core-8.0.37.jar:8.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.37.jar:8.0.37]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.0.37.jar:8.0.37]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.37.jar:8.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.37.jar:8.0.37]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.2.8.RELEASE.jar:4.2.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.8.RELEASE.jar:4.2.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.37.jar:8.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.37.jar:8.0.37]
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:89) ~[spring-web-4.2.8.RELEASE.jar:4.2.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.8.RELEASE.jar:4.2.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.37.jar:8.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.37.jar:8.0.37]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) ~[spring-web-4.2.8.RELEASE.jar:4.2.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.8.RELEASE.jar:4.2.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.37.jar:8.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.37.jar:8.0.37]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121) ~[spring-web-4.2.8.RELEASE.jar:4.2.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.8.RELEASE.jar:4.2.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.37.jar:8.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.37.jar:8.0.37]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) ~[tomcat-embed-core-8.0.37.jar:8.0.37]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) [tomcat-embed-core-8.0.37.jar:8.0.37]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) [tomcat-embed-core-8.0.37.jar:8.0.37]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) [tomcat-embed-core-8.0.37.jar:8.0.37]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.0.37.jar:8.0.37]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) [tomcat-embed-core-8.0.37.jar:8.0.37]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528) [tomcat-embed-core-8.0.37.jar:8.0.37]
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1100) [tomcat-embed-core-8.0.37.jar:8.0.37]
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:687) [tomcat-embed-core-8.0.37.jar:8.0.37]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520) [tomcat-embed-core-8.0.37.jar:8.0.37]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476) [tomcat-embed-core-8.0.37.jar:8.0.37]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_51]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_51]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.0.37.jar:8.0.37]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_51]mysql
2017-04-07 13:02:50.306 ERROR 6276 --- [nio-8080-exec-1] org.apache.velocity : ResourceManager : unable to find resource 'error.html' in any resource loader.web
既然是空指針異常,說明是個人代碼哪裏有問題,因此查看代碼是否真的有空指針異常:spring
代碼以下:sql
@Controller
public class HomeController {
@RequestMapping(path = {"/index"}, method = {RequestMethod.GET, RequestMethod.POST})
public String index(){
return "index";
}
}
測試運行的代碼就這麼一小部分,不該該有空指針異常的,可是,它又提示我,是空指針異常,因此,我很無奈的,一個個排查其餘的代碼是否有異常。認真看了其餘代碼,發現沒有什麼錯誤。
因而,開始百度,得知,多是spring那些註解沒有寫上去,致使底層報錯。又將全部的spring註解之類的代碼所有檢查了遍,確保都正確後,仍是無效。
最後,採用最笨的方法,將代碼一行行的從新敲,每寫一個代碼,就所有測試一遍,終於,發現了致使空指針異常的真正bug所在:
在配置文件application.properties:
spring.velocity.suffix=.html
spring.datasource.url=jdbc:mysql://localhost:3306/wenda?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=1234
mybatis.config-location=classpath:mybatis-config.xml
spring.velocity.toolbox-config-location=toolbox.xml
引入了最後一個文件,可是,在項目中忘記添加了,由於暫時還不須要這個功能,因此,因爲項目中缺失這個配置文件,它找不到,所以,報空指針異常。
總結:
1.空指針異常,查看控制檯的輸出,控制檯中沒有一行提示我本身寫的代碼哪裏出錯誤了,所以,必定不是我本身 寫的代碼的問題,而是,spring底層機制的問題,這個時候就須要去思考原理,
接下來,排除代碼的可能性,那就剩下的是spring配置文件的問題了,仔細查看配置文件之間的聯繫,確保正確無誤後,就應該沒啥問題了。
這個bug雖然很簡單,可是,本身也花費了差很少一天的時間才解決。主要是,空指針異常,讓我思路跑偏了,一直在找本身代碼哪裏有錯。根本緣由,一方面本身編程不細心,另外一方面,本身寫代碼經驗
還不夠豐富,控制檯輸出的異常信息還不是很會利用。
2.whitelabel error page異常必定是有緣由的,好比,訪問路徑不對,解析不對,註解忘記引入等。另外,對於springboot項目,
WendaApplication這個類要放在groupId對應的包下,其餘本身新建的包都應該是它的一個子包。注意,目錄結構的關聯。