[英]Runtime Exception while instantiating Server class for rmiregistry
[英]Throws Exception while instantiating class
我有一個非常簡單的問題-
我有一個名為DEClient
的類,其構造函數如下:
public DEClient(List<DEKey> keys) {
process(keys);
}
DEKey
類是這樣的-
public class DEKey {
private String name;
private String value;
public DEKey(){
name = null;
value = null;
}
public DEKey(String name, String value){
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
現在,我試圖實例化DEClient
構造函數。 因此,我需要擁有List<DEKey>
。
所以我要做的是使用service.getKeys()
(將返回String
)和id
作為值實例化DEKey
類,如下所示。
DEKey dk = new DEKey(service.getKeys(), id);
//The below line throws exception whenever I am running.
DEClient deClient = new DEClient((List<DEKey>) dk);
我在這里做什么錯?
您需要首先創建一個List
,然后將密鑰添加到該List
。 由於DEKey
不是 List
,因此像您一樣進行鑄造不是實現的方法,並且對其進行鑄造將拋出ClassCastException
。
DEKey dk = new DEKey(service.getKeys(), id);
List<DEKey> list = new ArrayList<DEKey>();
list.add (dk);
DEClient deClient = new DEClient(list);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.