Java 使用 ConcurrentHashMap 避免空指针异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18736257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Avoid nullpointer exception with ConcurrentHashMap
提问by rickygrimes
I know this question has been asked several times, but I didn't find any right answer for the question. How do you avoid NullPointerException from being thrown when you fetch results from a ConcurrentHashMap. I have the below code that is throwing me a NullPointer.
我知道这个问题已经被问过好几次了,但我没有找到这个问题的任何正确答案。当您从 ConcurrentHashMap 获取结果时,如何避免抛出 NullPointerException。我有下面的代码向我抛出一个 NullPointer。
public static String getPersonInfo(String personAddress) {
//if(concMap!=null && concMap.containsKey(personAddress))
return concMap.get(personAddress);
//return null;
}
In the concurrent hashmap,I am storing person id and address in a concurrent hashmap. In a few cases, the id is null. Although I do a null check before putting the data in the map, I still get the below error while trying to read the data.
在并发散列图中,我将人员 ID 和地址存储在并发散列图中。在少数情况下,id 为空。尽管我在将数据放入地图之前进行了空检查,但在尝试读取数据时仍然出现以下错误。
java.lang.NullPointerException
at java.util.concurrent.ConcurrentHashMap.get(Unknown Source)
Someone please help :( I have wasted over 3 hours trying to debug this. Although I see a lot of posts online, I am not very clear yet with what can be done to avoid it.
有人请帮忙:(我浪费了3个多小时试图调试这个。虽然我在网上看到很多帖子,但我还不是很清楚可以做些什么来避免它。
采纳答案by rickygrimes
Do a null check on personAddress, calling get(null) throws a NPE. It's even specified in the method documentation
对 personAddress 进行空检查,调用 get(null) 会抛出 NPE。它甚至在方法文档中指定
回答by Amin Abu-Taleb
Try this to avoid getting null key from the map:
试试这个以避免从地图中获取空键:
public static String getPersonInfo(String personAddress) {
//if(concMap!=null && concMap.containsKey(personAddress))
return (personAddress != null) ? concMap.get(personAddress) : null;
}
回答by weiglt
This is not possible. The APIstates, that this method only throws a NPE, if the key is null. You should use the debugger, somewhere your null-check is not performed. I also don't understand, why you posted the code with comments. What does that mean? Is that your null check? Why is it commented out?
这不可能。该API指出,这种方法仅抛出一个NPE,如果该键为null。您应该在不执行空检查的地方使用调试器。我也不明白,为什么您发布带有注释的代码。这意味着什么?那是你的空支票吗?为什么会被注释掉?
回答by Sanchit
If you use (or planning to use) JetBrains Idea, a Java ide, you can use some particular annotations developed by them.
如果您使用(或计划使用)JetBrains Idea,一个 Java ide,您可以使用他们开发的一些特定注释。
Basically, you've got @Nullable and @NotNull.
基本上,你有@Nullable 和@NotNull。
回答by wwwslinger
It's the keythat is null, which in this case is personAddress
(as everyone is saying). However, it would be safest to check them all:
这是空的关键,在这种情况下是personAddress
(正如每个人所说)。但是,最安全的做法是检查它们:
public static String getPersonInfo(String personAddress) {
if (personAddress != null && concMap != null && concMap.containsKey(personAddress)) {
return concMap.get(personAddress);
}
return null;
}
I should add that the ability for almost everything but primitives to be null is a common headache in Java. Most internal methods don't even bother dealing with it and just throw the NPE. If something can't be null down the road, check for this at your first meaningful opportunity..
我应该补充一点,除了原语之外,几乎所有东西都可以为 null 是 Java 中常见的问题。大多数内部方法甚至都懒得处理它,只是抛出 NPE。如果某些事情不能为空,请在您第一个有意义的机会时检查一下。