查询出对象为空做判断,但是查询为空直接报错空指针

java.lang.NullPointerException
at com.flyme.app.base.App_Reg.accountReg(App_Reg.java:88)
at com.flyme.app.base.App_Base.updateMobile(App_Base.java:102)
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:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)

图片说明

        Account account = accountService.findByMobile(mobile);
        //account查询出为null  但是报错空指针
        if (ObjectUtils.isEmpty(account)) {
            accountInfo.setAccountName(mobile).setPassword(password).setEmail(email);
            accountInfo.setUserName(userName);
            accountInfo.setRoleCode(RoleConstants.ROLE_VIP);
            initAccountInfo(accountInfo, AccountTypeEnum.Vip.getValue());
//service 方法
    @Override
    public Account findByMobile(String mobile) {
        if (ObjectUtils.isEmpty(mobile)) {
            return null;
        }
        CriterionMap map = new CriterionMap();
        map.add("mobile", mobile);
        return findByMap(map);
    }

控制台输出截图
图片说明

6个回答

App_Base.java:102,这行看看是做了什么操作、这里判断是没问题的,看看findByMobile方法内返回参数之前做的操作。

qq_21741673
洗洗脚泡泡澡 回复迟暮的小疯子: 那就把那个方法catch一下空指针异常、其他异常的话继续抛出去好了
10 个月之前 回复
qq_41991751
迟暮的小疯子 有log日志输出 sql 是正确的查询出的为空,换个存在的手几机好查询就没事。
10 个月之前 回复

accountService.findByMobile(mobile) 内部报错了debug,看一下

qq_40994492
qq_40994492 回复迟暮的小疯子: 亲,,解决了问题了吗
8 个月之前 回复
qq_41991751
迟暮的小疯子 回复Sam Sho: 这个方法进不去,下一步和进去方法都是进去那个异常类去了。
10 个月之前 回复
u012228718
Sam Sho 不是下一步,是进去(step in),进这个方法里面
10 个月之前 回复
qq_41991751
迟暮的小疯子 回复Sam Sho: 进去是这个异常类 InvocationTargetException
10 个月之前 回复
u012228718
Sam Sho 回复迟暮的小疯子: findByMobile 方法 denug 进去
10 个月之前 回复
qq_41991751
迟暮的小疯子 debug就是这里走不动了,空指针了。log日志查询也是正确的,sql都输出的有。换一个数据库存在的mobile参数值,就是正确的。只要为空才会报错
10 个月之前 回复

看看accountService是不是为空

少年,at com.flyme.app.base.App_Reg.accountReg(App_Reg.java:88),这个accountReg方法也就是你88行这里报空指针了。。。

估计是accountService没注入吧

首先在这里谢谢大家了,逻辑是不错的,我写的是两个APP 前端接口,在一个接口调用另外一个接口,直接实例化然后调用,就一直报空指针。真的是很愁人,一下午就解决了这么一个问题。
解决方案: 把实例化对象调用换成 httpclient 请求接口传递参数就行了。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!