Reputation: 19612
I have a very simple problem-
I have a class named DEClient
whose constructor is like this-
public DEClient(List<DEKey> keys) {
process(keys);
}
And DEKey
class is like this-
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;
}
}
Now I am trying to instantiate DEClient
constructor. So for that I need to have List<DEKey>
.
So what I did is I instantiated DEKey
class like below using service.getKeys()
(which will return String
) and id
as the value.
DEKey dk = new DEKey(service.getKeys(), id);
//The below line throws exception whenever I am running.
DEClient deClient = new DEClient((List<DEKey>) dk);
What wrong I am doing here?
Upvotes: 0
Views: 392
Reputation: 36449
You need to first make a List
, then add your key to that List
. Casting like you have done is not the way to do it, since DEKey
is not a List
and casting into it will throw a ClassCastException
.
DEKey dk = new DEKey(service.getKeys(), id);
List<DEKey> list = new ArrayList<DEKey>();
list.add (dk);
DEClient deClient = new DEClient(list);
Upvotes: 1