Introduction
I am having some difficulties with figuring out what's causing me to get a NullPointerException in a HashMap that should be fairly simple. To understand why I am using a HashMap I think the best way to describe it is to say that I have a Multithreaded program which has some results I would like to store. In that context I have created a class called SaveMethod
which will be used to store values from the process. Since one never knows which thread that will finish first I use a HashMap
to store thread i
's value in this hashmap as follows:
Code:
First the definition of the HashMap:
// The first Integer element is the thread index, the second is the Double value
private static HashMap<Integer,Double> AUC = new HashMap<Integer,Double>();
Then I call this method ones a thread terminates for storage:
public static void setAUC(double AUC,int i){
Double AUCtemp = AUC;
SaveMethod.AUC.put(i,AUCtemp);
}
Some more description:
Now, I have previously had success implementing above functionality by the usage of:
private static HashMap<Integer,Double[]> AUC = new HashMap<Integer,Double[]>();
That is, compared to the previous example, Double[]
instead of Double
. And that has worked pretty well.
Question
My question is twofold: 1)
Why am I getting Exception in thread "main" java.lang.NullPointerException
when I try to call this code:
public static Double getAUC(int i){
double AUC = SaveMethod.AUC.get(i);
return AUC;
}
2) is this saving option thread safe?
Some additional reasoning why I need this:
let me try and justify why I need this storage option. I have a set of calculations that need to be performed, lets say 100. Each calculation takes approximately 10 seconds. Now, to reduce computational time I have implemented Multithread so that these calculations can be done simultanously. But these calculations does not depend on each other in any way. However, I need to store each result from each thread (which is a lot of different values/matrixes etc.) for each computation, so that the results can be reached globally from other methods/classes for plotting purpose. What do you recon is the way to store the results from the calculation AND maintain a thread-safe enviroment?
1) Why am I getting Exception in thread "main"
Because there's presumably no value for the given integer, so the get
call returns null
. You're then unboxing that, which throws an exception. Your code is effectively:
public static Double getAUC(int i){
Double temp = SaveMethod.AUC.get(i); // This can return null
double AUC = temp.doubleValue(); // This was auto-unboxing
Double boxed = Double.valueOf(AUC); // This was boxing
return boxed;
}
If you want to just return null
instead if the key is missing, just avoid ever converting to Double
. Your method can be as simple as:
public static Double getAUC(int i) {
return SaveMethod.AUC.get(i);
}
It's not clear whether you want to just return null
though, or throw some other kind of exception.
2) is this saving option thread safe?
Well HashMap
isn't thread-safe to start with, so no... consider using ConcurrentHashMap
instead.
See more on this question at Stackoverflow