My program is showing null pointer exception, and
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.ArrayList.add(Object)" because "<local6>[<local7>]" is null
at ThisIsTestForNothing.main(ThisIsTestForNothing.java:27)
Can anyone find where is mistake in my program. I am trying to resolve it from last 4 hours. My program is...
import java.util.*;
class TestClass {
public static void main(String args[] ) throws Exception {
Scanner scan = new Scanner(System.in);
int test=scan.nextInt();
scan.nextLine();
int size;
int steps;
int temp=0;
ArrayList<Integer> array[] = new ArrayList[test];
for(int j=0;j<test;j++){
size = scan.nextInt();
steps = scan.nextInt();
scan.nextLine();
for(int i=0;i<size;i++){
array[j].add(scan.nextInt());
}
for(int i=0;i<steps;i++){
temp = array[j].get(size-1);
array[j].remove(size-1);
array[j].add(0,temp);
}
if(j==test-1){
for(j=0;j<test;j++){
for(int i=0;i<size;i++){
System.out.print(array[j].get(i)+" ");
}
}
}
}
}
}
Input: The first line will consists of one integer T denoting the number of test cases. For each test case:
- The first line consists of two integers N and K, N being the number of elements in the array and K denotes the number of steps of rotation.
- The next line consists of N space separated integers , denoting the elements of the array A. Output: Print the required array.