0

I am setting up the Backend code of a chat messaging system for an app my group is creating using WebSockets. My goal is for our app to be able to send and receive messages in a public group chat, and also specifically Direct Message (DM) specific people with an @ symbol, in front of the recipient's username.

I managed to get the public group messaging component working perfectly fine. However, I am running into an issue with the DM functionality. Let's say for example a person named "test" wrote a message to someone named "teacher" and here was what they typed: "@teacher testMessage". Ideally, I would want the program to send the message "testMessage" to "teacher" only. However, every time I would test the program using Postman, I would end up with the following error:

java.lang.NullPointerException: Cannot invoke "javax.websocket.Session.getBasicRemote()" because the return value of "java.util.Map.get(Object)" is null

From my understanding, the error means that the variable type the method is supposed to receive (in this case a String), is not what it is actually getting.

Here is the code below:

private static Map < Session, String > sessionUsernameMap = new Hashtable<>();
private static Map < String, Session > usernameSessionMap = new Hashtable<>();

    
    @OnMessage
    public void onMessage(Session session, String message) throws IOException { //The message is the the entire thing that the person types (ex: @teacher testMessage)
        logger.info("Message Received: " + message); //String message = @teacher testMessage
        String username = sessionUsernameMap.get(session); //String username = "test" This is the username of the person who wrote the message
        if (message.startsWith("@")) {
            String destUsername = message.split(" ")[0].substring(1);   //destUsername = "teacher"
            String realMessage = message.substring(message.lastIndexOf(" ") + 1); //realMessage = "testMessage"
            sendMessageToParticularUser(destUsername, "[DM] " + username + ": " + realMessage); //puts in "teacher" for destUsername and "testMessage" for realMessage
            sendMessageToParticularUser(username, "[DM] " + username + ": " + message);
        }
        else {
            broadcast(username + ": " + message);
        }
        
        msgRepo.save(new Message(username, message));
    }

    private void sendMessageToParticularUser(String username, String message) {
        System.out.println(message);
        try {
            usernameSessionMap.get(username).getBasicRemote().sendText(message); //PROBLEM RIGHT HERE WITH .get(username)
        } catch (IOException e) {
            logger.info("Exception: " + e.getMessage().toString());
            e.printStackTrace();
        }
    }

I have been working on this issue for a few hours now with no luck. I would very much appreciate any help or input on this. Thank you.

Kyodi
  • 31
  • 8
  • "From my understanding, the error means that the variable type the method is supposed to receive (in this case a String), is not what it is actually getting." No. That would be a classcast exception. A NullPointerException means that you are trying to dereference a null. – tgdavies Dec 05 '21 at 06:30
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – tgdavies Dec 05 '21 at 06:30
  • Thanks for your response. I checked that out and Im understanding NullPointerExceptions a little better. However, I don't seem to quite understand how it can apply here. I checked each declared String variable (destUsername, username, message, etc.) with a println as the method was continuing, and not once has it ever come up null. My guess is that it turns into a null variable once moving to the sendToAParticularUser method, but im not sure why it's coming up null – Kyodi Dec 05 '21 at 18:34
  • What the exception is telling you is that `usernameSessionMap.get(username)` is `null`, so for some reason that `username` does not have a `Session` in the `Map`. – tgdavies Dec 05 '21 at 23:30

0 Answers0