Java 如何将bytearray存储为hashmaps中的值?

Java 如何将bytearray存储为hashmaps中的值?,java,android,hashmap,linkedhashmap,Java,Android,Hashmap,Linkedhashmap,我试图在LinkedHashMap中存储bytearray static Map<Long, byte[]> lhm2 = new LinkedHashMap<Long, byte[]>(1000); 将抛出NullPointerException。lhm2包含键1,我用 if(lhm2.get(1) != null){ System.out.println("not null"); } 有什么建议吗 提前谢谢 克里斯你应该做: if(lhm2.contain

我试图在LinkedHashMap中存储bytearray

static Map<Long, byte[]> lhm2 = new LinkedHashMap<Long, byte[]>(1000);
将抛出NullPointerException。lhm2包含键1,我用

if(lhm2.get(1) != null){
    System.out.println("not null");
}
有什么建议吗

提前谢谢

克里斯

你应该做:

if(lhm2.contains(1){
    lhm2.get(1)
}

你的钥匙应该是长的,所以最好这样做

lhm2.get(1L)
而不是像这样做

lhm2.get(1)

您应该使用
contansKey(keyName)
检查密钥是否存在。您何时获得NPE?请展示一个简短但完整的程序来演示您的问题。我强烈怀疑它不是你想象的那样。只有当
lhm2
为空时,该代码才会失败。
lhm2.get(1)
只有在尝试使用此对象调用方法时才能抛出NPE感谢您的快速支持!下面的评论修复了我的问题,如果我想将其放入for循环,该怎么办?简单的演员阵容可以吗?像lhm2.get((long)i)?检查此链接以在地图上迭代
lhm2.get(1)