I have written a simple code to read a 2d integer array of size n1 and n2 and print the middle element in java.
import java.io.*;
import java.lang.*;
import java.util.*;
public class Main {
public static void main(String args[])throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n1=Integer.parseInt(br.readLine());
int n2=Integer.parseInt(br.readLine());
int[][] arr=new int[n1][n2];
System.out.println("n1"+n1+"n2="+n2);
String[] sarr=new String[n2];
for(int i=0;i<n1;i++) {
sarr = br.readLine().split(" ");//line 13
for (int j = 0; j < n2; j++)
arr[i][j] = Integer.parseInt(sarr[j]);
}
System.out.println(arr[n1/2][n2/2]);
}
}
I was getting the below error in an online compiler but when executed in intellijIDEA there was no error.
Exception in thread "main" java.lang.NullPointerException
at Main.main(Main.java:13)