【问题标题】:Java HashMap is becoming null after exiting from if condition退出 if 条件后,Java HashMap 变为空
【发布时间】:2022-01-24 20:12:35
【问题描述】:

在第一个 if 条件中将一个值放入 HashMap 中,然后在下一个 if 条件下,HashMap 为空。

    HashMap<String, List> operation = new HashMap<>();
    List method = Arrays.asList("get","put","post");
    ArrayList <String> params = new ArrayList<String>();
     for (Map.Entry<String, Object> entry : map.entrySet()) {
        if(entry.getKey().contains("/") && !entry.getKey().contains("$ref")){
            currentKey = entry.getKey();
            operation.put(entry.getKey(),params); // Here value is assigned successfully
        }
        if(method.contains(entry.getKey())){
             operation.get(currentKey).add(entry.getKey()); //Here its null point exception & HashMap has no data
        }
       
        if (entry.getValue() instanceof Map) {
            iterate((Map<String, Object>) entry.getValue());
        } 
    }

【问题讨论】:

    标签: java hashmap


    【解决方案1】:

    您与currentKey 不一致并且您的第二个if 是错误的。您正在测试method,但随后调用operation。让我们解决这个问题。喜欢,

    HashMap<String, List<String>> operation = new HashMap<>();
    List<String> method = Arrays.asList("get", "put", "post");
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        String currentKey = entry.getKey();
        if (currentKey.contains("/") && !currentKey.contains("$ref")) {
            operation.put(currentKey, new ArrayList<>());
        }
        if (operation.containsKey(currentKey)) {
            operation.get(currentKey).add(currentKey);
        }
    
        if (entry.getValue() instanceof Map) {
            iterate((Map<String, Object>) entry.getValue());
        }
    }
    

    【讨论】:

    • 感谢您的快速回答,在第二个如果 - 我的测试是基于方法(列表)如果匹配添加到操作 HashMap 的现有键。注意:当我在类变量中声明 HashMap 时,它工作正常,但在本地面临这个问题。我的问题是为什么在第二个条件下 HashMap 在第一个条件下为其分配了一个值时为空(我想我错过了一些概念性的东西)。提前致谢
    【解决方案2】:

    这就是问题所在:

     for (Map.Entry<String, Object> entry : map.entrySet()) {
        if (entry.getKey().contains("/") 
            && !entry.getKey().contains("$ref")) {
            currentKey = entry.getKey();
            operation.put(entry.getKey(), params); // Here value is assigned
                                                   // successfully
        }
        if (method.contains(entry.getKey())) {
             operation.get(currentKey).add(entry.getKey()); 
                                                   // Here its null pointer
                                                   // exception & HashMap has no data
        }
        ...
     }
    

    第一个测试是测试键的值。第二个测试是测试密钥是否包含在methods 中。这些测试是不同的。

    根据您向我们展示的代码,这些测试的结果实际上有 4 种可能性:

    • 两项测试均成功
    • 两个测试都失败了
    • 第一次测试成功,第二次测试失败
    • 第一次测试失败,第二次测试成功

    最后一种可能是NPE的原因。

    如果(例如)键不包含字符串"/",但method 字符串(或集合或其他)确实包含键,那么:

    1. operation.put... 语句未执行
    2. operation.get... 语句已执行...并且将 NPE,因为 operation.get 调用返回 null

    第二个问题加剧了这种情况。当第一个测试失败时,它不会执行currentKey = entry.getKey(); 语句。这意味着operation.get(currentKey) 将使用上一步中的currentKey。该值甚至可以是null。 (请注意,operation.get(null) 很可能会返回 null。)


    简而言之,您的代码似乎包含至少两个错误,对于缺少operation 映射条目最合理的解释是您的代码没有将它放在那里! (它不是成为null。)

    (我们不需要假设 HashMap 中的缺陷来解释这一点。)

    如果这不能让您满意地解释问题,您需要在您的问题中添加minimal reproducible example...以便我们自己运行。

    【讨论】:

      猜你喜欢
      • 2020-11-02
      • 1970-01-01
      • 2016-09-03
      • 1970-01-01
      • 2012-12-07
      • 1970-01-01
      • 2018-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多