MessageBroker 中的空指针异常
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 thismsgBroker.routeMessageToService(msg, null)
throws NullPointerException
.
Can anyone give best solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
msgBroker 为 null,或者传递给 msgBroker.routeMessageToService() 的参数之一为 null。如果没有捕获异常时打印的堆栈跟踪,则很难判断问题出在哪里 - 有了它,它将准确识别哪个变量不应该为空。
从您的代码来看,考虑到 msg 不能为 null(否则异常会更早发生),要么 msgBroker 为 null,传递给 msgBroker 的 null 正在造成损害,要么 msgBroker 内部的代码。 RouteMessageToService() 有一个错误。
您的代码的更强大的版本是:
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:
唯一可能的解释是您的 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.
我的应用程序中遇到了同样的问题。空指针异常是由于 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.