This code finds the frequency of elements in the array. But it is showing cannot read field because array is null.
Code
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner lta = new Scanner(System.in);
Count[] arr = new Count[101];
System.out.println("Enter the elements :");
for (int i = 0;i < 10;i++){
int value = lta.nextInt();
arr[value].count += 1;
}
System.out.println("Frequency of the elements in the array :");
for (int i = 1;i < arr.length;i++){
if (arr[i].count > 0){
System.out.print(i + " : " + arr[i].count);
System.out.println();
}
}
}
static class Count{
long value;
long count;
}
}
Error
Exception in thread "main" java.lang.NullPointerException: Cannot read field "count" because "arr[value]" is null
Is there any solution for this program to run without any error or what is the reason behind termination of the JAVA program.