2015-12-19 72 views
-1

我試圖做的Eclipse IDE一個WebSocket的溝通,當我跑我的代碼我得到一個NullPointerException。我檢查和getAttribute方法的名稱是一樣的,在豆的HttpSession的getAttribute()返回null

package ws; 

import java.io.IOException; 
import java.util.Set; 
import java.util.concurrent.CopyOnWriteArraySet; 

import javax.websocket.server.ServerEndpoint; 

import fundstarter.model.ConnectToRMIBean; 

import javax.servlet.http.HttpSession; 
import javax.websocket.*; 

@ServerEndpoint(value="/ws", configurator = HandShake.class) 
public class WebSocketAnnotation { 

    private Session session; 
    private ConnectToRMIBean sessionUser; 
    private HttpSession httpSession; 
    private static final Set<WebSocketAnnotation> myConnections = new CopyOnWriteArraySet<WebSocketAnnotation>(); 

    public WebSocketAnnotation() { 

    } 

    @OnOpen 
    public void start(Session session, EndpointConfig config) { 

     this.session = session; 
     this.httpSession = (HttpSession) config.getUserProperties().get(HttpSession.class.getName()); 
     myConnections.add(this); 
     this.sessionUser = (ConnectToRMIBean) httpSession.getAttribute("RMIBean"); 
     //sendMessage("New message"); 
    } 

    @OnClose 
    public void end() { 
     // clean up once the WebSocket connection is closed 
     myConnections.remove(this); 
    } 

    @OnMessage 
    public void receiveMessage(String message) { 
     sendMessage(message); 
    }  

    @OnError 
    public void handleError(Throwable t) { 
     t.printStackTrace(); 
    }  

    private void sendMessage(String text) { 
     try { 
      System.out.println("[WebSocketAnnot]RMIBean User Id -> " + this.sessionUser.getUserID()); 
      this.session.getBasicRemote().sendText(text); 

     } catch (IOException e) { 
      try { 
       this.session.close(); 
      } catch (IOException e1) { 
       e1.printStackTrace(); 
      } 
     } 
    } 
} 

誰能告訴我什麼是我的錯誤?

回答

-1

我發現的錯誤。這是HandShake上的modifyHandshake方法的拼寫錯誤