The code:
public class main {
public static void countFrequencies(ArrayList<String> list,int n)
{
    int k=0;
    Map<String, Integer> hm = new HashMap<String, Integer>();
    for (String i : list) {
        Integer j = hm.get(i);
        hm.put(i, (j == null) ? 1 : j + 1);
    }
    for (Map.Entry<String, Integer> val : hm.entrySet()) {
        System.out.println("Element " + val.getKey() + " "
                + "occurs"
                + ": " + val.getValue() + " times");
        k++;
        if(k==n)
            break;
    }
}
public static void main(String[] args)  {
    String nr1;
    ArrayList<String> nr = new ArrayList<String>();
    Scanner obj= new Scanner(System.in);
    System.out.println("Input n");
    int n= obj.nextInt();
    System.out.println("Input k ");
    int k= obj.nextInt();
    System.out.println("Input n elemente: ");
    for(int i=1;i<=n;i++)
    {
            nr1=obj.nextLine();
            System.out.println("Element " + (i));
            nr.add(nr1);
    }
    countFrequencies(nr,k);
}
}
If I run the code everything goes ok until I input the last element in the list, if do It appears as null, it simply doesn't let me input it, also when I print the frequencies list I need it to show the top k elements after the number of frequencies, in the descending order.
 
    