[英]Java Interface-Based Polymorphism Not Being Called
我正在開發一個將命令作為字符串數據接收並將其傳遞給HashMap
定義的正確處理程序的程序。
將命令傳遞給正確的處理程序的代碼如下:
//This is run in its own thread, hence the run() method
@Override
public void run() {
try {
//Socket is a Socket object containing the client's connection
InputStreamReader is = new InputStreamReader(socket.getInputStream());
//MAX_MESSAGE_SIZE is an integer, specifically 1024
char[] data = new char[MAX_MESSAGE_SIZE];
is.read(data, 0, MAX_MESSAGE_SIZE);
String strData = new String(data);
//Split on UNIX or Windows type newline
String[] lines = strData.split("\\r?\\n");
//First verb determines the command
String command = (lines[0].split(": "))[0];
//Re-add the ": " because the HashMap will look for it
if (handlerMap.containsKey((command + ": "))) {
System.err.println("Got command: " + command);
AbstractHandler handler = handlerMap.get(command);
System.err.println("Passing connection + data to handler...");
handler.handleData(socket, lines);
System.err.println("Handler is done!");
} else {
System.err.println("Didn't understand command: " + command);
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
outputStream.writeBytes(Protocol.UNKNOWN_ERROR);
outputStream.flush();
socket.close();
}
} catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
}
}
HashMap
的值部分是實現接口AbstractHandler
的對象。 AbstractHandler
定義了一個方法: handleData(Socket s, String[] lines)
。 供參考,這是地圖的初始化位置:
public RequestManager(Socket socket) {
this.socket = socket;
handlerMap = new HashMap<>();
handlerMap.put(Protocol.USERNAME, new AuthenticationHandler());
//Set arg to true b/c it is a set request, not a change request
handlerMap.put(Protocol.SET_PASSWORD, new ChangePasswordHandler(true));
handlerMap.put(Protocol.CHANGE_PASSWORD, new ChangePasswordHandler());
handlerMap.put(Protocol.FORGOT_PASSWORD, new ForgotPasswordHandler());
}
並且對象中的所有handleData
方法僅包含以下代碼:
@Override
public void handleData(Socket s, String[] lines) {
clientSocket = s; //clientSocket field in class
System.err.println("Made it into handler");
}
奇怪的是,在顯示“將連接+數據傳遞給處理程序”后,什么都沒有發生。 我沒有看到有關進入處理程序的任何信息,也沒有看到處理程序完成的異常或消息。 是什么原因造成的?
您測試以查看是否存在處理程序
if (handlerMap.containsKey((command + ": "))) {
但是你嘗試得到一個處理程序
AbstractHandler handler = handlerMap.get(command);
因此,如果它與鍵CommandName:
一樣存在,則可能不會通過鍵CommandName
獲得它。 因此,您將在調用handler.handleData(socket, lines);
上有一個未經檢查的NullPointerException
handler.handleData(socket, lines);
你的可跑者會死得很慘。
看來您需要更改第一個或第二個。 假設您說可以看到它顯示“正在將連接+數據傳遞給處理程序...”,我認為您需要將其更改為:
AbstractHandler handler = handlerMap.get(command + ": ");
在處理地圖時,如果進行小的樣式更改,就可以防止這種情況在現在和將來對您造成影響。 如果找不到鍵,則 Map.get
返回null
,因此您可以執行以下操作:
AbstractHandler handler = handlerMap.get(command + ": ");
if (handler != null) {
/* ... */
handler.handleData(socket, lines);
System.err.println("Handler is done!");
} else {
/* ... */
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.