So this piece of code –
public static boolean check(HashMap<Character,Integer> hmbase, HashMap<Character,Integer> hmtemp){
for( Character key : hmbase.keySet() ){
if( !hmtemp.containsKey(key) ) return false;
else{
if( hmbase.get(key) > hmtemp.get(key)) return false;
}
}
return true;
}
is a Java method intended to compare two HashMap<Character, Integer> objects (hmbase and hmtemp). The method returns true if every key in hmbase is present in hmtemp and the value for each key in hmtemp is greater than or equal to the corresponding value in hmbase. If any key in hmbase is missing in hmtemp, or if any value in hmtemp is less than in hmbase, the method returns false.
But I am getting error in this line –
if( hmbase.get(key) > hmtemp.get(key)) return false;
particularly in hmtemp.get(key), it is showing null pointer exception which is impossible because I am never passing null for hmtemp and I am already checking if the key is present in hmtemp in the line above it. How can this error be occuring? Can you give solutions.
My error looks like this –
Hangup (SIGHUP)
Exception in thread "main" java.lang.NullPointerException
at Solution.check(GFG.java:79)
at Solution.smallestWindow(GFG.java:48)
at GFG.main(GFG.java:15)
(79th line is if( hmbase.get(key) > hmtemp.get(key)) return false;)
Thank you for your solutions in advance!
I tried to check if hmtemp is null before starting this look itself.
if(hmtemp == null) return false;
Still it is giving the same error.
Ayush is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.