Home > front end >  Java HashMap is becoming null after exiting from if condition
Java HashMap is becoming null after exiting from if condition

Time:12-24

In first if condition putting a value in HashMap & in next if condition the HashMap is null.

    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());
        } 
    }

CodePudding user response:

You aren't being consistent with currentKey and your second if is wrong. You are testing method but then invoking on operation. Let's fix that. Like,

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());
    }
}

CodePudding user response:

This is where the problems are:

 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
    }
    ...
 }

The first test is testing the value of the key. The second test is testing whether the key is contained in methods. These tests are different.

Based on the code that you have shown us, there are actually 4 possibilities for the outcomes of these tests:

  • both tests succeed
  • both tests fail
  • first test succeeds, and second test fails
  • first test fails, and second test succeeds

The last possibility is the probably the cause of the NPE.

If (for example) the key does not contain the string "/", but it the method string (or collection or whatever) does contain the key, then:

  1. The operation.put... statement IS NOT executed
  2. The operation.get... statement IS executed ... and will NPE because the operation.get call returns null.

And there is this is exacerbated by a second problem. When the first test fails, it won't perform the currentKey = entry.getKey(); statement. That means that operation.get(currentKey) will be using a currentKey value from a previous step. The value could even be null. (Note that operation.get(null) will most likely return null.)


In short, your code appears to contain at least two bugs, and the most plausible explanation for the missing operation map entry is that your code didn't put it there! (It is not becoming null.)

(We do not need to hypothesize a flaw in HashMap to explain this.)

If this doesn't explain the problem to your satisfaction, you will need to add a minimal reproducible example to your question ... so that we can run it ourselves.

  • Related