Fixed bug in countOccurences method

Added a check to see if the target object is null before performing comparisons and fixed the way the program checks for equality of existing elements and the target.