2013-03-06 73 views
0

我現在不是Java無法理解錯誤的地方。使用Java 7從對象未經檢查轉換爲Java 7

public void chatMessage(String userName, String message) { 
     IScope scope = Red5.getConnectionLocal().getScope(); 
     ISharedObject chat = getSharedObject(scope, "chat"); 
     List<ChatHistoryItem> history = (List<ChatHistoryItem>) chat.getAttribute("remoteHistory"); 
     ChatHistoryItem item = new ChatHistoryItem(); 
     item.user = userName; 
     item.date = new Date(); 
     item.message = message; 
     history.add(item); 
     chat.setAttribute("remoteHistory", history); 
    } 

錯誤:Unchecked cast from Object to List<ChatHistoryItem>

回答

1

的問題是,因爲你不能安全地確定將由ISharedObject.getAttribute方法調用返回的類型。出現在該行:

List<ChatHistoryItem> history = (List<ChatHistoryItem>) chat.getAttribute("remoteHistory"); 

如果此方法返回的object的類型是不List<ChatHistoryItem>您將收到一條ClassCastException。如果該方法確實返回適當的類型,您的代碼仍將執行。

我認爲這不是一個錯誤打破你的代碼,它只是從你的IDE使用警告?