title: How do I inject an event into a servlet in JavaEE7?
body: <p>I have a <code>@ServerEndpoint</code> that adds web socket sessions and I want it to <code>@Observes</code> an event that will be fired by a servlet and then broadcast a message to all the web sockets. </p>
  
  <p>I get a <code>NullPointerException</code> in my servlet when I try to use the event that was defined with <code>@Inject</code>. What am i doing wrong?</p>
  
  <p>WebSocket Handler:</p>
  
  <pre><code>    import javax.ejb.Asynchronous;
      import javax.enterprise.event.Observes;
      import javax.enterprise.inject.Any;
      import javax.websocket.*;
      import javax.websocket.server.ServerEndpoint;
      import java.util.Collections;
      import java.util.HashSet;
      import java.util.Set;
  
      @ServerEndpoint("/events")
      public class WebSocketHandler
      {
          //This holds our sessions for async retrieval
          private static final Set&lt;Session&gt; sessions = Collections.synchronizedSet( new HashSet&lt;Session&gt;() );
  
          @OnOpen
          public void open(Session session)
          {
              //Add to our collection
              sessions.add( session );
          }
  
          @OnClose
          public void close(Session session, CloseReason closeReason)
          {
              //Just remove it
              sessions.remove( session );
          }
  
          ...elided...
  
          @Asynchronous
          public void notify(@Observes @Any TestEvent event)
          {
              synchronized( sessions )
              {
                  //Loop through and send to all listeners
                  for ( Session session : sessions )
                  {
                      //Make sure it's still open
                      if ( session.isOpen() )
                      {
                          try
                          {
                              session.getBasicRemote().sendText( event.getMessage() );
                          }
                          catch (Exception e)
                          {
                              e.printStackTrace();
                          }
                      }
                  }
              }
          }
      }
  </code></pre>
  
  <p>Event type:</p>
  
  <pre><code>    import java.io.Serializable;
  
      public class TestEvent implements Serializable
      {
          private String message;
  
          public TestEvent()
          {
          }
  
          public TestEvent(String message)
          {
              this.message = message;
          }
  
          public void setMessage(String message)
          {
              this.message = message;
          }
  
          public String getMessage()
          {
              return message;
          }
  
          @Override
          public String toString()
          {
              return "TestEvent: " + message;
          }
      }
  </code></pre>
  
  <p>Servlet:</p>
  
  <pre><code>    import javax.ejb.Asynchronous;
      import javax.enterprise.event.Event;
      import javax.inject.Inject;
      import javax.servlet.ServletException;
      import javax.servlet.annotation.WebServlet;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import java.io.IOException;
  
      @WebServlet("/broadcast")
      public class SendMessageServlet extends HttpServlet
      {
          @Inject
          Event&lt;TestEvent&gt; testEvent;
  
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
          {
              //Fire the event (THIS "testEvent" IS NULL)
              testEvent.fire( new TestEvent( "{\"message\":\"An event occurred at " + System.currentTimeMillis() + "\",\"status\":200}" ) );
          }
      }
  </code></pre>
  
  <p>beans.xml:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;beans xmlns="http://java.sun.com/xml/ns/javaee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
          http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"&gt;
      &lt;/beans&gt;
  </code></pre>
  
  <p>pom.xml:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;project xmlns="http://maven.apache.org/POM/4.0.0"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
          &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
          &lt;groupId&gt;groupId&lt;/groupId&gt;
          &lt;artifactId&gt;TestWebSocket&lt;/artifactId&gt;
          &lt;version&gt;1.0&lt;/version&gt;
  
          &lt;packaging&gt;war&lt;/packaging&gt;
  
          &lt;!-- Tell Maven what language version to use --&gt;
          &lt;properties&gt;
              &lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt;
              &lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt;
          &lt;/properties&gt;
  
          &lt;dependencies&gt;
  
              &lt;!-- Enables the annotations, etc needed --&gt;
              &lt;dependency&gt;
                  &lt;groupId&gt;javax&lt;/groupId&gt;
                  &lt;artifactId&gt;javaee-api&lt;/artifactId&gt;
                  &lt;version&gt;7.0&lt;/version&gt;
              &lt;/dependency&gt;
  
          &lt;/dependencies&gt;
  
      &lt;/project&gt;
  </code></pre>