为啥我在 Spring Boot 服务上使用 notnoop apns push 得到 NullPointerException?

Posted

技术标签:

【中文标题】为啥我在 Spring Boot 服务上使用 notnoop apns push 得到 NullPointerException?【英文标题】:Why am I getting NullPointerException using notnoop apns push on Spring Boot Service?为什么我在 Spring Boot 服务上使用 notnoop apns push 得到 NullPointerException? 【发布时间】:2020-03-03 11:16:05 【问题描述】:

尝试使用notnoop apns 发送推送时遇到异常,

java.lang.NullPointerException
    at java.util.regex.Matcher.getTextLength(Matcher.java:1283)
    at java.util.regex.Matcher.reset(Matcher.java:309)
    at java.util.regex.Matcher.<init>(Matcher.java:229)
    at java.util.regex.Pattern.matcher(Pattern.java:1093)
    at com.notnoop.apns.internal.Utilities.decodeHex(Utilities.java:116)
    at com.notnoop.apns.EnhancedApnsNotification.<init>(EnhancedApnsNotification.java:72)
    at com.notnoop.apns.internal.AbstractApnsService.push(AbstractApnsService.java:54)
    at com.notnoop.apns.internal.ApnsServiceImpl.push(ApnsServiceImpl.java:36)
    at com.notnoop.apns.internal.AbstractApnsService.push(AbstractApnsService.java:45)
    at com.myapp.health.srbd.service.PushService.sendPush(PushService.java:25)
    at com.myapp.health.srbd.controller.TestController.addUser(TestController.java:59)
    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:190)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1639)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    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)

我的 Spring Boot,服务代码,

public String sendPush(PushToken token) 

    System.out.println("token = " + token.getMessage());

    ApnsService service = APNS.newService().withCert("Certificates.p12", "12345678")
         .withSandboxDestination()
         .build();

     String payload = APNS.newPayload()
         .alertBody(token.getMessage())
         .alertTitle(token.getTitle())
         .build();

     System.out.println("payload: " + payload);

     try 
         service.push(token.getToken(), payload);
     catch(Exception e) 
         System.out.println("Exception occured: " + e);
         e.printStackTrace();
         return e.getMessage();
     

     System.out.println("The message has been hopefully sent");
     return "Success";

PushToken模型,

public class PushToken 

    private String title;
    private String token;
    private String message;


    public String getTitle() 
        return title;
    
    public void setTitle(String title) 
        this.title = title;
    
    public String getToken() 
        return token;
    
    public void setToken(String token) 
        this.token = token;
    
    public String getMessage() 
        return message;
    
    public void setMessage(String message) 
        this.message = message;
    

我该如何解决?

【问题讨论】:

【参考方案1】:

当我将空值作为设备令牌传递时发生错误。使用有效的非空设备令牌解决了问题。

【讨论】:

以上是关于为啥我在 Spring Boot 服务上使用 notnoop apns push 得到 NullPointerException?的主要内容,如果未能解决你的问题,请参考以下文章

为啥我用 spring-boot 得到 404 休息

为啥我在 Spring-boot 应用程序中通过 SessionFactory 有循环依赖?

为啥我在使用 Spring Boot Application 的邮递员中没有收到错误消息

用户''@'localhost'的访问被拒绝(使用密码:NO)spring-boot

为啥 import.sql 在 Spring Boot 中会失败?

为啥我的表单没有在 Spring Boot 中传递信息