null pointer exception when retrieving from HashMap even though containsKey() returns true

旧巷老猫 提交于 2019-12-13 02:09:29

问题


I've been developing an Android guitar app, and currently I'm having some problems with HashMap.

To sum it up, I get a nullpointer exception when I try to retrieve a value from a HashMap with get(key) despite containsKey(key) returning true for that key.

Specifically, I have an instance _currentPreset of a class, which has a HashMap called _maxCoordRange, which is initialized in the class like this:

private Map<String, Float> _maxCoordRange = new HashMap<String, Float>();

This parameter is not modified in the constructors of the class. Instead, it can only be modified by the following method:

public void setMaxCoordRange(String parName, float value) {
    _maxCoordRange.put(parName, value);
}

and accessed by:

public float getMaxCoordRange(String parName) {
    return _maxCoordRange.get(parName);
}   

Once I've created the _currentPreset instance, I set some values to the _maxCoordRange map like this:

for (int i = 0; i < _currentPreset.getCoordiates().size(); i++) {       
    _currentPreset.setMaxCoordRange(_currentPreset.getParNames().get(i), 0.75f);
}

In other parts of the code I do the following:

if(_currentPreset.getMaxCoordRange().containsKey(pareterName)){
    cord.setMaxValueCoord(_currentPreset.getMaxCoordRange(pareterName));
}

Here is the odd part. I get a nullpointer exeption when I try to retrieve the value corresponding to the pareterName from the HashMap with getMaxCoordRange, despite containsKey(key) returning true.

I did some debugging and I found something rather strange to me. When I finished putting the values in the map, in the expressions tag (when in debugging mode in eclipse), the table looks like this:

table   HashMap$HashMapEntry[16]  (id=829660787832) 
[0] null    
[1] null    
[2] null    
[3] null    
[4] null    
[5] null    
[6] null    
[7] null    
[8] HashMap$HashMapEntry  (id=829660788568) 
[9] HashMap$HashMapEntry  (id=829660786456) 
[10]    HashMap$HashMapEntry  (id=829660787920) 
[11]    HashMap$HashMapEntry  (id=829660788376) 
[12]    HashMap$HashMapEntry  (id=829660787448) 
[13]    HashMap$HashMapEntry  (id=829660787640) 
[14]    HashMap$HashMapEntry  (id=829660786840) 
    hash    789729998   
    key "Test Par3" (id=829660737240)   
    next    HashMap$HashMapEntry  (id=829660786144) 
        hash    789729854   
        key "Test Par1" (id=829660736816)   
        next    null    
        value   Float  (id=829660786088)    
    value   Float  (id=829660786824)    
[15]    HashMap$HashMapEntry  (id=829660787088) 
    hash    789729999   
    key "Test Par4" (id=829660737376)   
    next    HashMap$HashMapEntry  (id=829660786648) 
        hash    789729855   
        key "Test Par2" (id=829660737104)   
        next    null    
        value   Float  (id=829660786632)    
    value   Float  (id=829660787016)    

There should be 10 values, and there are, but 2 of them are in the next field of elements 14 and 15. I read that this is the way the hashMaps arrange the data in order to be more efficient, but couldn't it be the reason why I'm not able to retrieve the values?

Sorry about giving so much information, but I don't really know where the problem is. I've used HashMaps before and I didn't have this problem.

Any ideas on how can i solve this and finally get my values from the map?


回答1:


It could be you're using "get()" with both indices and string.

Here, you are assuming that indices match up linearly with your table. As your debug shows, they do not. You've set it up to be accessed via Strings, but you're using the index.

for (int i = 0; i < _currentPreset.getCoordiates().size(); i++) {       
_currentPreset.setMaxCoordRange(_currentPreset.getParNames().get(i), 0.75f);
}

Here, you are using pareterName, whatever that is. I hope it's a String

if(_currentPreset.getMaxCoordRange().containsKey(pareterName)){
            cord.setMaxValueCoord(_currentPreset.getMaxCoordRange(pareterName));
        }

For HashMaps to be effective, stick with accessing the data via your String as you've defined it to be:



来源:https://stackoverflow.com/questions/20888170/null-pointer-exception-when-retrieving-from-hashmap-even-though-containskey-re

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!