目录
- int初始化可以为0,但不能为NULL
- 要进行判断后赋值
- 总结
int初始化可以为0,但不能为NULL
今天在制作网页的时候,出现一个空指针错误,但是找了半天没有发现,后来发现是int 赋值的时候变成了空值:
要进行判断后赋值
if(aLikesCount.getLikesNumber()!=null){ //int 不能为NULL LikesCountNumber=aLikesCount.getLikesNumber(); }
严重: Servlet.service() for servlet JSP threw exception
Java.lang.NullPointerExceptionat org.apache.jsp.index_jsp._jspService(index_jsp.java:803)at org.apache.jASPer.runtime.HttpJspBase.service(HttpJspBase.java:70)at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:96)at struts2.action.MyEditorStrutsFilter.doFilter(MyEditorStrutsFilter.java:23)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:719)at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:465)at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:390)at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:317)at org.apache.struts2.dispatcher.ServletDispatcherResult.doExecute(ServletDispatcherResandroidult.java:164)at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:191)at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:369)at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:273)at org.apache.struts2.interceptor.DeprecationInterceptor.intercept(DeprecationInterceptor.java:41)at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:167)at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:76)at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:229)at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:229)at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191)at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73)at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)at org.apache.struts2.interceptor.DateTextFieldInterceptor.intercept(DateTextFieldInterceptor.java:125)at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91)at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:253)at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:139)at com.opensymphony.xwork2.DefaultActionInvocation.invokphpe(DefaultActionInvocation.java:244)at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193)at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.interchttp://www.devze.comept(ExceptionMappingInterceptor.java:189)at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)at org.apachwww.devze.come.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:564)at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81)at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)at struts2.action.MyEditorStrutsFilter.doFilter(MyEditorStrutsFilter.java:23)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:151)at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)at org.apache.catalina.core.Ap编程客栈plicationFilterChain.doFilter(ApplicationFilterChain.java:206)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:521)at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)at org.apache.coyote.AbstractProtocolA b s t r a c t C o n n e c t i o n H a n d l e r . p r o c e s s ( A b s t r a c t P r o t o c o l . j a v a : 674 ) a t o r g . a p a c h e . t o m c a t . u t i l . n e t . N i o E n d p o i n t AbstractConnectionHandler.process(AbstractProtocol.java:674) at org.apache.tomcat.util.net.NioEndpointAbstractConnectionHandler.process(AbstractProtocol.java:674)atorg.apache.tomcat.util.net.NioEndpointSocketProcessor.doRun(NioEndpoint.java:1500)at org.apache.tomcat.util.net.NioEndpointS o c k e t P r o c e s s o r . r u n ( N i o E n d p o i n t . j a v a : 1456 ) a t j a v a . u t i l . c o n c u r r e n t . T h r e a d P o o l E x e c u t o r . r u n W o r k e r ( U n k n o w n S o u r c e ) a t j a v a . u t i l . c o n c u r r e n t . T h r e a d P o o l E x e c u t o r SocketProcessor.run(NioEndpoi开发者_Python教程nt.java:1456) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutorSocketProcessor.run(NioEndpoint.java:1456)atjava.util.concurrent.ThreadPoolExecutor.runWorker(UnknownSource)atjava.util.concurrent.ThreadPoolExecutorWorker.run(Unknown Source)at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)at java.lang.Thread.run(Unknown Source)
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。
精彩评论