为什么这个Java代码为现有的键值打印null?

为什么这个Java代码为现有的键值打印null?,java,collections,nullpointerexception,Java,Collections,Nullpointerexception,此程序为现有键值返回null 代码: 文档类的代码 @XmlRootElement public class Document { private Map<String, String> values = new HashMap<String, String>(); //Getter and setter for values } @XmlRootElement 公共类文档{ 私有映射值=新的HashMap(); //值的Getter和setter } 包含值

此程序为现有键值返回null

代码:

文档类的代码

@XmlRootElement
public class Document {
private Map<String, String> values = new HashMap<String, String>();


//Getter and setter for values


}
@XmlRootElement
公共类文档{
私有映射值=新的HashMap();
//值的Getter和setter
}
包含值的文档文件,这些值填充到文档对象中

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<document>
<values>
<entry>
    <key>240400MAHAR</key>
    <value>100010101</value>
</entry>
<entry>
    <key>100300IDG</key>
    <value>44444444</value>
</entry>
<entry>
    <key>200200MDM</key>
    <value>11221321</value>
</entry>
</values>
</document>

240400马哈尔
100010101
100300IDG
44444444
200200DM
11221321

一种可能性是,由
document.getValues()
print
String
返回的
Map
中使用的关键对象类似于
100300IDG
,但实际上不是
String
实例。因此,
map.get(“100300IDG”)
正在比较
String
对象和非String对象(反之亦然),并返回
null
,因为找不到任何对象

这一切都取决于如何实现
document.getValues()
。例如

private static Map<String, String> getValues() {
    Map map = new HashMap<>();
    map.put(new MyClass("100300IDG"), "44444444");
    return map;
} 
...
// with  
public class MyClass {
    private String string;

    public MyClass(String string) {
        this.string = string;
    }

    public String toString() {
        return string;
    }
}
但实际上不包含键为“100300IDG”的条目

确保您的密钥类型匹配。编译器在这里做不了什么



或者另一个线程会删除打印和获取之间的条目。

一种可能是
document.getValues()
print
String
等值返回的
Map
中使用的关键对象实际上不是
String
实例。因此,
map.get(“100300IDG”)
正在比较
String
对象和非String对象(反之亦然),并返回
null
,因为找不到任何对象

这一切都取决于如何实现
document.getValues()
。例如

private static Map<String, String> getValues() {
    Map map = new HashMap<>();
    map.put(new MyClass("100300IDG"), "44444444");
    return map;
} 
...
// with  
public class MyClass {
    private String string;

    public MyClass(String string) {
        this.string = string;
    }

    public String toString() {
        return string;
    }
}
但实际上不包含键为“100300IDG”的条目

确保您的密钥类型匹配。编译器在这里做不了什么



或者另一个线程会在打印和获取之间删除条目。

您确定字符串实际上是相同的吗?请说明如何填充
getValues()返回的映射
。映射实例是什么类型的?@nikpon它是HashMap。@SotiriosDelimanolis映射由Jabx xml解析器填充,您确定字符串实际上是相同的吗?请说明如何填充由
getValues()返回的映射
。映射实例是什么类型的?@nikpon它是HashMap。@SotiriosDelimanolis映射由Jabx xml填充parser@DavidWallace它使用(你会得到警告),但未经检查的转换对对象没有任何作用,只对引用有作用。是的,谢谢。我意识到这有点太晚了。在看到你的回复之前,我实际上删除了我的评论。回答正确+1。@DavidWallace它使用(你会得到警告),但未经检查的转换对对象没有任何作用,只对引用有作用。是的,谢谢。我意识到这有点太晚了。在看到你的回复之前,我实际上删除了我的评论。好答案+1。
private static Map<String, String> getValues() {
    Map map = new HashMap<>();
    map.put(new MyClass("100300IDG"), "44444444");
    return map;
} 
...
// with  
public class MyClass {
    private String string;

    public MyClass(String string) {
        this.string = string;
    }

    public String toString() {
        return string;
    }
}
{100300IDG=44444444}