-4

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:

  1. 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.
  2. The next line consists of N space separated integers , denoting the elements of the array A. Output: Print the required array.
Adriaan
  • 17,741
  • 7
  • 42
  • 75
Tirath Sharma
  • 93
  • 1
  • 8
  • I suggest reading entire lines, then splitting and parsing ints, as necessary – OneCricketeer Mar 24 '21 at 16:55
  • 2
    Please read up on how to make a [MRE] – peer Mar 24 '21 at 16:56
  • `new ArrayList[test]` creates an array of size `test`, that are all null. `array[j].add(anything)` therefore causes an NPE... No part of the question asks for an array of lists. Since you have defined sizes, you should use a 2D array – OneCricketeer Mar 24 '21 at 16:58
  • but how is it possible to achieve this from 2D array, this is an improved version of my code please see [link](https://stackoverflow.com/q/66783617/14886384) In which someone gave me answer to do this with array and i then explained him why i can't. – Tirath Sharma Mar 24 '21 at 17:03
  • 1
    You only need a 1D array (as the answer to your previous question said). – k314159 Mar 24 '21 at 17:18
  • What do monks have to do with this question? (see title) – NomadMaker Mar 24 '21 at 17:27
  • monk and rotation is a question name. I already specified in comments in answer of previous question that why that is not the correct answer. – Tirath Sharma Mar 24 '21 at 17:33
  • If you're using an odd question title, this should be explained in the question body. One downvote. – NomadMaker Mar 24 '21 at 17:50
  • Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under the [CC BY-SA 4.0 license](https://creativecommons.org/licenses/by-sa/4.0/), for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted. If you want to know more about deleting a post please see: [How does deleting work?](https://meta.stackexchange.com/q/5221) – Adriaan Nov 22 '22 at 13:03
  • @Adriaan Can you please upvote to delete this question. I am not able to delete it now. I asked this question so many times ago. – Tirath Sharma Nov 22 '22 at 13:05

1 Answers1

1

new ArrayList[test] creates an array of size test, that are all null. array[j].add(anything) therefore causes an NPE

If you insist on using arraylist (which I don't think you need because each "test" iteration is an isolated problem, so you don't need to store historical/future test results), you must initialize each array index before you can ever add anything to each list

for(int j=0;j<test;j++){
    array[j] = new ArrayList<>();
}

However, as mentioned, you should do something like this

int tests;
// read tests 
for(int test = 0; test < tests; test++) {
    int N, K;
    // read in N and K
    int[] A = new int[N];
    // read the line into elements of A 
    rotate(A, K); // rotate the array, K places 
    System.out.println(Arrays.toString(A));
} 
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245