title: throwing org.atmosphere.cpr.AtmosphereResourceImpl: Exception during suspend() operation java.lang.NullPointerException
body: <p>I am new to <a href="/questions/tagged/atmosphere" class="post-tag" title="show questions tagged &#39;atmosphere&#39;" rel="tag">atmosphere</a>-websocket. I am working on private chat. I am using <a href="/questions/tagged/dropwizard" class="post-tag" title="show questions tagged &#39;dropwizard&#39;" rel="tag">dropwizard</a> framework.</p>
  
  <p>Below is my code:</p>
  
  <p><strong>service.java</strong>:</p>
  
  <pre><code>@Override
  public void run(ServiceConfiguration config, Environment environment)throws Exception {         
      AtmosphereServlet servlet = new AtmosphereServlet();
      servlet.framework().addInitParameter("com.sun.jersey.config.property.packages", "dk.cooldev.chatroom.resources.websocket");
      servlet.framework().addInitParameter(ApplicationConfig.WEBSOCKET_CONTENT_TYPE, "application/json");
      servlet.framework().addInitParameter(ApplicationConfig.WEBSOCKET_SUPPORT, "true");
      ServletRegistration.Dynamic servletHolder = environment.servlets().addServlet("Chat", servlet);
      servletHolder.addMapping("/chat/*");``
  }
  </code></pre>
  
  <p><strong>ChatRoom.java</strong>:</p>
  
  <pre><code>package resource;    
  import org.atmosphere.config.service.DeliverTo;
  import org.atmosphere.config.service.Disconnect;
  import org.atmosphere.config.service.ManagedService;
  import org.atmosphere.config.service.Message;
  import org.atmosphere.config.service.PathParam;
  import org.atmosphere.config.service.Ready;
  import org.atmosphere.cpr.AtmosphereResource;
  import org.atmosphere.cpr.AtmosphereResourceEvent;
  import org.atmosphere.cpr.AtmosphereResourceFactory;
  import org.atmosphere.cpr.Broadcaster;
  import org.atmosphere.cpr.BroadcasterFactory;
  import org.atmosphere.cpr.MetaBroadcaster;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  import representation.UserMessage;
  import utility.ChatProtocol;
  import utility.JacksonEncoder;
  import utility.ProtocolDecoder;
  import utility.UserDecoder;
  
  import javax.inject.Inject;
  
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.concurrent.ConcurrentHashMap;
  
  @ManagedService(path = "/chat/{room: [a-zA-Z][a-zA-Z_0-9]*}")
  public class ChatRoom {
      private final Logger logger = LoggerFactory.getLogger(ChatRoom.class);
  
      private final ConcurrentHashMap&lt;String, String&gt; users = new ConcurrentHashMap&lt;String, String&gt;();
  
      private final static String CHAT = "/chat/";
  
      @PathParam("room")
      private String chatroomName;
  
      @Inject
      private BroadcasterFactory factory;
  
      @Inject
      private AtmosphereResourceFactory resourceFactory;
  
      @Inject
      private MetaBroadcaster metaBroadcaster;
  
      /**
       * Invoked when the connection as been fully established and suspended, e.g ready for receiving messages.
       *
       * @param r
       */
      @Ready(encoders = {JacksonEncoder.class})
      @DeliverTo(DeliverTo.DELIVER_TO.ALL)
      public ChatProtocol onReady(final AtmosphereResource r) {
          r.suspend();
          logger.info("Browser {} connected.", r.uuid());
          return new ChatProtocol(users.keySet(), getRooms(factory.lookupAll()));
      }
  
      private static Collection&lt;String&gt; getRooms(Collection&lt;Broadcaster&gt; broadcasters) {
          Collection&lt;String&gt; result = new ArrayList&lt;String&gt;();
          for (Broadcaster broadcaster : broadcasters) {
              if (!("/*".equals(broadcaster.getID()))) {
                  // if no room is specified, use ''
                  String[] p = broadcaster.getID().split("/");
                  result.add(p.length &gt; 2 ? p[2] : "");
              }
          };
          return result;
      }
  
      /**
       * Invoked when the client disconnect or when an unexpected closing of the underlying connection happens.
       *
       * @param event
       */
      @Disconnect
      public void onDisconnect(AtmosphereResourceEvent event) {
          if (event.isCancelled()) {
              // We didn't get notified, so we remove the user.
              users.values().remove(event.getResource().uuid());
              logger.info("Browser {} unexpectedly disconnected", event.getResource().uuid());
          } else if (event.isClosedByClient()) {
              logger.info("Browser {} closed the connection", event.getResource().uuid());
          }
      }
  
      /**
       * Simple annotated class that demonstrate how {@link org.atmosphere.config.managed.Encoder} and {@link org.atmosphere.config.managed.Decoder
       * can be used.
       *
       * @param message an instance of {@link ChatProtocol }
       * @return
       * @throws IOException
       */
      @Message(encoders = {JacksonEncoder.class}, decoders = {ProtocolDecoder.class})
      public ChatProtocol onMessage(ChatProtocol message) throws IOException {
  
          if (!users.containsKey(message.getAuthor())) {
              users.put(message.getAuthor(), message.getUuid());
              return new ChatProtocol(message.getAuthor(), " entered room " + chatroomName, users.keySet(), getRooms(factory.lookupAll()));
          }
  
          if (message.getMessage().contains("disconnecting")) {
              users.remove(message.getAuthor());
              return new ChatProtocol(message.getAuthor(), " disconnected from room " + chatroomName, users.keySet(), getRooms(factory.lookupAll()));
          }
  
          message.setUsers(users.keySet());
          logger.info("{} just send {}", message.getAuthor(), message.getMessage());
          return new ChatProtocol(message.getAuthor(), message.getMessage(), users.keySet(), getRooms(factory.lookupAll()));
      }
  
      @Message(decoders = {UserDecoder.class})
      public void onPrivateMessage(UserMessage user) throws IOException {
          String userUUID = users.get(user.getUser());
          if (userUUID != null) {
              // Retrieve the original AtmosphereResource
              AtmosphereResource r = resourceFactory.find(userUUID);
  
              if (r != null) {
                  ChatProtocol m = new ChatProtocol(user.getUser(), " sent you a private message: " + user.getMessage().split(":")[1], users.keySet(), getRooms(factory.lookupAll()));
                  if (!user.getUser().equalsIgnoreCase("all")) {
                      factory.lookup(CHAT + chatroomName).broadcast(m, r);
                  }
              }
          } else {
              ChatProtocol m = new ChatProtocol(user.getUser(), " sent a message to all chatroom: " + user.getMessage().split(":")[1], users.keySet(), getRooms(factory.lookupAll()));
              metaBroadcaster.broadcastTo("/*", m);
          }
      }
  
  }
  </code></pre>
  
  <p>Now, when I run my application , its throwing </p>
  
  <blockquote>
    <p>resource.ChatRoom: Browser 72196f81-3425-4137-8f37-c5aa6b134534 connected.
    WARN: org.atmosphere.cpr.AtmosphereResourceImpl: Exception during suspend() operation java.lang.NullPointerException</p>
  </blockquote>
  
  <p>Please help me out to resolve this exception.</p>