-3

import java.util.Scanner; import java.io.*;

class AraOfDigit{

public static void main(String[] args) throws IOException{
  Scanner sc =new Scanner(System.in);

BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter wr = new PrintWriter(System.out);

int iteration = sc.nextInt();
int count = 0;
while(count<iteration){
     int n = sc.nextInt();
     int mod = sc.nextInt();
     String arr [] = new String[n];

     ****String[] arr_A =br.readLine().split(" ");****//Nullpointer Exception How to slove?
       for(int i=0;i<n;i++) {
        arr[i]=arr_A[i];
        }
      
    StringBuilder total = new StringBuilder();
    for(int j=0;j<n;j++){
      total.append(arr[j]);
    }
     int num = Integer.parseInt(total.toString());
    num = num/10;
     int op = num%mod;
   System.out.println(op);

  count++;
   }

}

}

  • I can guess that your `br.readLine()` call returns null that means that no string was inputed. So, when you call `split()` method of null object you get NPE – Looken Apr 15 '21 at 18:13

1 Answers1

0

You are reading the next line when you have reached the end. So there's nothing to read anymore and you get null for br.readLine().

Take a look at Javadoc:

Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached

You can check if a line is null:

String line = br.readLine();
if (line != null) {
    String[] arr_A =.split(" ");
    //...
}
Japhei
  • 565
  • 3
  • 18