即使containsKey()返回true,也从HashMap中获取空指针异常 [英] null pointer exception when retrieving from HashMap even though containsKey() returns true

查看:436
本文介绍了即使containsKey()返回true,也从HashMap中获取空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发Android吉他应用程序,目前我在 HashMap 中遇到了一些问题。



<总结一下,当我尝试从 HashMap 中检索 get时,出现空指针异常 (key)尽管 containsKey(key)返回 true 为该键。



具体来说,我有一个类的实例 _currentPreset ,它有一个 HashMap 调用 _maxCoordRange ,它在类中被初始化为这样:

 私人地图< String,Float> _maxCoordRange = new HashMap< String,Float>(); 

这个参数在类的构造函数中没有修改。相反,它只能通过以下方法修改:

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

以及访问方式:

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

一旦我创建了 _currentPreset instance,我为这个 _maxCoordRange 地图设置了一些值:

  _currentPreset.setMaxCoordRange(_currentPreset.getParNames()。get(i),0.75f); 
}

在代码的其他部分,我执行以下操作:

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

这里有一些奇怪的部分。当我尝试从 HashMap 中检索与 pareterName 对应的值时,我得到一个空指针豁免c> getMaxCoordRange ,尽管 containsKey(key)返回true。



调试,我发现了一些相当奇怪的东西。
当我把地图中的值放入地图时,在表达式标记中(当在eclipse中处于调试模式时),表格如下所示:

  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
keyTest Par1(id = 829660736816)
next null
值Float(id = 829660786088)
value Float(id = 829660786824)
[15] HashMap $ HashMapEntry(id = 829660787088)
hash 789729999
keyTest Par4 829660737376)
next HashMap $ HashMapEntry(id = 829660786648)
hash 789729855
keyTest Par2(id = 829660737104)
next null
value Float(id = 829660786632)
value Float(id = 829660787016)

应该有10个值,是,但其中2个在元素14和15的 next 字段中。我读到这是hashMaps安排数据以提高效率的方式,但难道这不是我无法检索这些值的原因吗?



对于提供这么多信息感到抱歉,但我不知道问题出在哪里是。我之前使用过HashMaps,并没有遇到这个问题。



关于如何解决这个问题并最终从地图中获取我的值的任何想法?

解决方案

可能你正在使用带指数和字符串的get()。

在这里,您假设索引与您的表格呈线性匹配。正如你的调试显示,他们不。您已将它设置为通过字符串访问,但您使用的是索引。

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

在这里,您正在使用pareterName,无论如何。我希望它是一个字符串

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

$ / code>

为了使HashMaps有效,坚持在您的String中通过访问数据已经定义为:

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?

解决方案

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:

这篇关于即使containsKey()返回true,也从HashMap中获取空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆