Home  >  Q&A  >  body text

java - The websocket server's onMessage() method reports a null pointer exception as soon as it operates the database.

I want to use websocket to implement the new message reminder function. For example, a posts an article and someone replies to a. When a goes online, a message reminder will appear, as shown below

If you directly use session.getBasicRemote().sendText("haha"); in the onMessage() method in the background, it can run normally, but a null pointer exception occurs when operating the database..


Front-end js processing:

Has anyone encountered the same problem? Please let me know..

曾经蜡笔没有小新曾经蜡笔没有小新2680 days ago1103

reply all(2)I'll reply

  • 仅有的幸福

    仅有的幸福2017-06-17 09:18:37

    This null pointer has nothing to do with weosocket. It should be a problem with the dependency injection of your messageService instance.
    You should check whether your MessageService is hosted by an IOC container, that is, whether the class is annotated with @Service or @Component.

    reply
    0
  • 黄舟

    黄舟2017-06-17 09:18:37

    During the development process, a class that is not managed by spring (such as MessageController) wants to call a method of an object that is managed by spring (such as MessageService object). If we call the method like the following code, a null pointer will appear. abnormal.

    Solution:

    Use ApplicationHelper to get the required MessageService object

    reply
    0
  • Cancelreply