In the following code, I am unable to initialize the BigInteger array due to which a NullPointerException is thrown as I compile:
public class Solution {
public static BigInteger[] arr;
public Solution(int n){
arr=new BigInteger[n];
for(BigInteger each:arr){
each=BigInteger.ZERO;
}
}
public static BigInteger maxlist(){
BigInteger max=arr[0];
for(BigInteger elem:arr)
if(elem.compareTo(max)>0)
max=elem;
return max;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
Solution s=new Solution(n);
for(int a0 = 0; a0 < m; a0++){
int a = in.nextInt();
int b = in.nextInt();
BigInteger k = in.nextBigInteger();
for(int i=a-1;i<=b-1;i++)
arr[i]=arr[i].add(k); //Error is thrown here
}
System.out.println(maxlist());
in.close();
}
}
Error:
Exception in thread "main" java.lang.NullPointerException
at Solution.main(Solution.java:29)
Is there any other way to do it?