英文:
Cannot add element to Map - Unsupported Operation Exception
问题
我在向这个可变映射中插入元素时遇到了问题,但我无法找出原因所在。
我可以看到它返回一个映射,所以理论上应该没有问题放置元素。
你能帮我解决这个问题吗?
myClient.authentications.put("basicAuthentication", httpBasicAuth)
认证看起来是这样的:
public Map<String, Authentication> getAuthentications() {
return authentications;
}
private Map<String, Authentication> authentications;
我是不是漏掉了什么东西?
编辑 1:
为了更清楚起见,添加一些代码
HttpBasicAuth只是一个实现了Authentication接口的基本类
public class HttpBasicAuth implements Authentication {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) {
if (username == null && password == null) {
return;
}
headerParams.put("Authorization", Credentials.basic(
username == null ? "" : username,
password == null ? "" : password));
}
val httpBasicAuth = HttpBasicAuth()
httpBasicAuth.username = "user"
httpBasicAuth.password = "pass"
myClient.authentications.put("basicAuthentication", httpBasicAuth)
英文:
I am having trouble inserting element to this mutable Map and i cannot figure out why?
I can see it returns Map so it should not an issue to put element in.
Can you help me with that?
myClient.authentications.put("basicAuthentication", httpBasicAuth)
Authentications look like this:
public Map<String, Authentication> getAuthentications() {
return authentications;
}
private Map<String, Authentication> authentications;
Is there something that i am missing here?
Edit 1:
Adding some more code for clearance
HttpBasicAuth is just a basic class that implements Authentication
public class HttpBasicAuth implements Authentication {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) {
if (username == null && password == null) {
return;
}
headerParams.put("Authorization", Credentials.basic(
username == null ? "" : username,
password == null ? "" : password));
}
val httpBasicAuth = HttpBasicAuth()
httpBasicAuth.username = "user"
httpBasicAuth.password = "pass"
myClient.authentications.put("basicAuthentication", httpBasicAuth)
答案1
得分: 0
当前,默认情况下,此字段具有 null 值,因此无法对其调用任何方法。所以当你调用:
myClient.authentications.put("basicAuthentication", httpBasicAuth)
时,你试图在 null 上调用 put() 方法。
要解决此问题,您可以将初始化从:
private Map<String, Authentication> authentications;
更改为(在Java中):
private Map<String, String> authentications = new HashMap<>();
或者(在Kotlin中):
private val authentications: Map<String, Authentication> = mapOf()
在进行这些更改后,此字段将被初始化为 empty array 而不是 null。请记住,当您基于它执行某些逻辑时(例如,您正在检查 authentications == null),这种更改非常重要。
英文:
Currently, by default this field has null value so you CAN NOT call any method on it. So when you call:
myClient.authentications.put("basicAuthentication", httpBasicAuth)
you are trying call method put() on null.
To solve it, you can change your initialization from:
private Map<String, Authentication> authentications;
To (in Java):
private Map<String, String> authentications = new HashMap<>();
or in to (in Kotlin):
private val authentications: Map<String, Authentication> = mapOf();
After those changes this field will be initialized with empty array instead of null. Remember that this change it's very important when you have some logic based on it (e.g. you are checking authentications == null).
专注分享java语言的经验与见解,让所有开发者获益!

评论