From 47e327f2e672d508ba99d841cfd721add9514c64 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Mon, 20 Oct 2014 21:12:51 +0200 Subject: [PATCH] Fix NPE in MultiUserChatManager.getMultiUserchat() Since the HashMap has WeakReferences as values, there are two levels at which values could be null. --- .../smackx/muc/MultiUserChatManager.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatManager.java index c1863c915..d4838d134 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatManager.java @@ -150,11 +150,20 @@ public class MultiUserChatManager extends Manager { * multi-user chat service is running. Make sure to provide a valid JID. */ public synchronized MultiUserChat getMultiUserChat(String jid) { - MultiUserChat multiUserChat = multiUserChats.get(jid).get(); - if (multiUserChat == null) { - multiUserChat = new MultiUserChat(connection(), jid, this); - multiUserChats.put(jid, new WeakReference(multiUserChat)); + WeakReference weakRefMultiUserChat = multiUserChats.get(jid); + if (weakRefMultiUserChat == null) { + return createNewMucAndAddToMap(jid); } + MultiUserChat multiUserChat = weakRefMultiUserChat.get(); + if (multiUserChat == null) { + return createNewMucAndAddToMap(jid); + } + return multiUserChat; + } + + private MultiUserChat createNewMucAndAddToMap(String jid) { + MultiUserChat multiUserChat = new MultiUserChat(connection(), jid, this); + multiUserChats.put(jid, new WeakReference(multiUserChat)); return multiUserChat; }