MessageBroker 中的空指针异常

发布于 2024-11-14 09:46:04 字数 716 浏览 4 评论 0原文

while (true) {  

    Message message = consumer.receive();

    if (message != null) {
        if (message instanceof TextMessage) {
        try{
        TextMessage textMessage = (TextMessage) message;
        System.out.println("Received message '"+ textMessage.getText() + "'");
        msg.setTimestamp(System.currentTimeMillis());
        msg.setBody(textMessage.getText());
        msgBroker.routeMessageToService(msg, null);

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

        } else {
        break;
        }
    }
}

在尝试运行这个时 msgBroker.routeMessageToService(msg, null) 抛出 NullPointerException

谁能给出最好的解决方案吗?

while (true) {  

    Message message = consumer.receive();

    if (message != null) {
        if (message instanceof TextMessage) {
        try{
        TextMessage textMessage = (TextMessage) message;
        System.out.println("Received message '"+ textMessage.getText() + "'");
        msg.setTimestamp(System.currentTimeMillis());
        msg.setBody(textMessage.getText());
        msgBroker.routeMessageToService(msg, null);

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

        } else {
        break;
        }
    }
}

while trying to run this
msgBroker.routeMessageToService(msg, null) throws NullPointerException.

Can anyone give best solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅笑轻吟梦一曲 2024-11-21 09:46:04

msgBroker 为 null,或者传递给 msgBroker.routeMessageToService() 的参数之一为 null。如果没有捕获异常时打印的堆栈跟踪,则很难判断问题出在哪里 - 有了它,它将准确识别哪个变量不应该为空。

从您的代码来看,考虑到 msg 不能为 null(否则异常会更早发生),要么 msgBroker 为 null,传递给 msgBroker 的 null 正在造成损害,要么 msgBroker 内部的代码。 RouteMessageToService() 有一个错误。

您的代码的更强大的版本是:

while (true) {  
    Message message = consumer.receive();
    //Note extra variables being checked here
    if (message != null && msg != null && msgBroker != null) {
        if (message instanceof TextMessage) {
            try{
                TextMessage textMessage = (TextMessage) message;
                System.out.println("Received message '"+ textMessage.getText() + "'");
                msg.setTimestamp(System.currentTimeMillis());
                msg.setBody(textMessage.getText());
                msgBroker.routeMessageToService(msg, null);
            } catch(Exception e){
                e.printStackTrace();
            }
        } else {
            break;
        }
    }
}

Either msgBroker is null or one of the parameters being passed into msgBroker.routeMessageToService() is. Without the stack trace printed when the exception is caught, it's hard to tell where the problem is - with it, it will identify exactly which variable is null when it shouldn't be.

From your code, and given the fact that msg can't be null (or the exception would have occurred earlier) it's that either msgBroker is null, the null being passed in to msgBroker is doing the damage, or that the code inside msgBroker.routeMessageToService() has a bug.

A more robust version of your code is:

while (true) {  
    Message message = consumer.receive();
    //Note extra variables being checked here
    if (message != null && msg != null && msgBroker != null) {
        if (message instanceof TextMessage) {
            try{
                TextMessage textMessage = (TextMessage) message;
                System.out.println("Received message '"+ textMessage.getText() + "'");
                msg.setTimestamp(System.currentTimeMillis());
                msg.setBody(textMessage.getText());
                msgBroker.routeMessageToService(msg, null);
            } catch(Exception e){
                e.printStackTrace();
            }
        } else {
            break;
        }
    }
}
韬韬不绝 2024-11-21 09:46:04

唯一可能的解释是您的 msgBroker 为 NULL。 msg 显然存在(否则您会更早收到错误),并且 null 是routeMessageToService 的第二个参数的允许值。

The only possible explanation is that your msgBroker is NULL. msg obviously exists (otherwise you'd get errors earlier) and null is an allowed value for the 2nd parameter to routeMessageToService.

红尘作伴 2024-11-21 09:46:04

我的应用程序中遇到了同样的问题。空指针异常是由于 messages-config.xml 中的安全约束引起的。在 messages-config.xml 文件中删除目的地的安全约束后,它对我有用。

I was having same issue in my application. The null pointer exception is due to security-constraint in messaging-config.xml. After removing security-constraint for the destination in messaging-config.xml file it worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文