【问题标题】:NullPointerException on SimpMessagingTemplate in SpringSpring 中 SimpMessagingTemplate 上的 NullPointerException
【发布时间】:2015-10-07 23:23:26
【问题描述】:

我正在构建一个应用程序,它将使用 STOMP 通过 websockets 发送消息。我想在没有客户端发出请求的情况下发送消息。根据documentation,我可以通过使用 convertAndSend 来做到这一点。

但是,当我尝试执行此操作时,我得到一个空指针异常。请看下面的代码:

public class ParseJSON {

    @Autowired
    private SimpMessagingTemplate template;

    public void getDetails(String json) {

       try {
            Tweet status = sendDetails(TwitterObjectFactory.createStatus(json));
            sentToWebApp(status);

        } catch (TwitterException e) {
            e.printStackTrace();
        }

    }

    @Scheduled(fixedDelay = 50)
    private void sentToWebApp(Tweet status) {
        System.out.println(status);
        this.template.convertAndSend("/tweet/update", status);
    }
}

堆栈跟踪:

java.lang.NullPointerException: null
    at org.myproject.worker.ParseJSON.sentToWebApp(ParseJSON.java:62)
    at org.myproject.worker.ParseJSON.getDetails(ParseJSON.java:51)
    at org.myproject.worker.TwitterClient.run(TwitterClient.java:50)
    at org.myproject.Controllers.TweetController.greeting(TweetController.java:37)

任何人都可以对我的情况进行说明,这样我就可以通过 websocket 发送消息而不会遇到异常。

提前致谢。

【问题讨论】:

  • template 从未被分配。
  • @PaulBoddington 请检查帖子中的链接,模板从未根据文档分配。
  • 在该示例中已分配。这就是this.template = template; 行的作用。
  • 我也有这个问题。这个答案对我帮助很大:stackoverflow.com/questions/19896870/…

标签: java spring spring-websocket spring-messaging


【解决方案1】:

您的模板变量未正确自动装配,这就是它给出 nullpointerException 的原因。

您可以检查弹簧配置以正确自动装配

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    • 2021-12-14
    • 2014-09-25
    • 1970-01-01
    相关资源
    最近更新 更多