无法向地图添加元素 – 不支持的操作异常

huangapple 未分类评论43阅读模式
英文:

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(&quot;basicAuthentication&quot;, httpBasicAuth)

Authentications look like this:

public Map&lt;String, Authentication&gt; getAuthentications() {
        return authentications;
}
private Map&lt;String, Authentication&gt; 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&lt;Pair&gt; queryParams, Map&lt;String, String&gt; headerParams) {
        if (username == null &amp;&amp; password == null) {
            return;
        }
        headerParams.put(&quot;Authorization&quot;, Credentials.basic(
            username == null ? &quot;&quot; : username,
            password == null ? &quot;&quot; : password));
    }

 val httpBasicAuth = HttpBasicAuth()
                httpBasicAuth.username = &quot;user&quot;
                httpBasicAuth.password = &quot;pass&quot;
                myClient.authentications.put(&quot;basicAuthentication&quot;, 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(&quot;basicAuthentication&quot;, httpBasicAuth)

you are trying call method put() on null.

To solve it, you can change your initialization from:

private Map&lt;String, Authentication&gt; authentications;

To (in Java):

private Map&lt;String, String&gt; authentications = new HashMap&lt;&gt;();

or in to (in Kotlin):

private val authentications: Map&lt;String, Authentication&gt; = 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).

huangapple
  • 本文由 发表于 2020年4月6日 22:15:47
  • 转载请务必保留本文链接:https://java.coder-hub.com/61061873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定