title: Publish and stop endpoint web service in Java
body: <p>I am trying to start/stop an Endpoint web service in a separate thread from my main program with the click of a button. Starting works fine, i am able to access all my WebMethods without issue. The problem is, when i click the stop button to try and stop the web service endpoint, i get an exception and i don't know what it is. I am new to Java as well.</p>
  
  <p>Thanks in advance.</p>
  
  <p>Excetion thrown at 'ep.stop()':</p>
  
  <pre><code>WebService Running On: http://0.0.0.0:9898/
  
          Exception in thread "Thread-0" java.lang.NullPointerException
              at com.sun.xml.internal.ws.transport.http.server.ServerMgr.removeContext(Unknown Source)
              at com.sun.xml.internal.ws.transport.http.server.HttpEndpoint.stop(Unknown Source)
              at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.stop(Unknown Source)
              at com.simba.Server.startWebSrv(Server.java:27)
              at com.simba.Server.run(Server.java:13)
  </code></pre>
  
  <p>Server class:</p>
  
  <pre><code>import javax.xml.ws.Endpoint;
  
  public class Server extends Thread {
  
        public volatile boolean active = true;
        private Endpoint ep; 
        private String ip = "0.0.0.0";
  
        public void run(){
              ep = Endpoint.publish("http://" + ip + ":9898/",  new SWS());
              startWebSrv();
        }
  
        private void startWebSrv(){
            synchronized(this){
                try {
                    while (active) {
                        System.out.println("WebService Running On: http://" + ip + ":9898/");
                        wait();
                    }
                }catch (InterruptedException e) {
                    e.printStackTrace();
                }finally{
                  if(!active){
                      ep.stop();
                      System.out.println("WebService Stopped!");
                  }
                }
            }
        }
  }
  </code></pre>
  
  <p>How i am attempting to stop the service/thread from my main program:</p>
  
  <pre><code>MenuItem mntmToggleWebservice = new MenuItem(menu_4, SWT.NONE);
          mntmToggleWebservice.addSelectionListener(new SelectionAdapter() {
              @Override
              public void widgetSelected(SelectionEvent e) {
                  synchronized(srv){
                      srv.active = false;
                      srv.notifyAll();            
                  }
              }
          });
          mntmToggleWebservice.setText("Toggle WebService");
  </code></pre>