+ 1

Why 'null' and NullPointerException is occurring?

1) int[] a =new int[3]; Sop(a); //[|@3e25a5 Sop(a[0]); //0 2) int[][] a =new int[2][3]; Sop(a); //[|@3e25a5 Sop(a); //[|@19821f Sop(a[0][0]); //0 3) int[][] a =new int[2][]; Sop(a); //[|@3e25a5 Sop(a[0]); //null Sop(a[0][0]); //R.E:NullPointerException

24th Jan 2022, 7:37 AM
Minhaj Haider
Minhaj Haider - avatar
13 odpowiedzi
+ 4
import java.util.Arrays; public class Program { public static void main(String[] args) { // int[][] a = new int[2][3]; // is shortcut to int[] array1 = {0,0,0}; int[] array2 = {0,0,0}; int[][] a = {array1, array2}; System.out.println( Arrays.deepToString(a)); // int[][] aa = new int[2][]; // is shortcut to int[][] aa = {null, null}; System.out.println( Arrays.deepToString(aa)); } }
24th Jan 2022, 2:04 PM
zemiak
+ 1
3rd example creates a[null, null] null[0] throws Exception
24th Jan 2022, 9:37 AM
zemiak
+ 1
Yes please any one explain
24th Jan 2022, 2:28 PM
Afzal Mehndi
0
Please explain more
24th Jan 2022, 11:05 AM
Minhaj Haider
Minhaj Haider - avatar
0
Tysm
24th Jan 2022, 2:13 PM
Minhaj Haider
Minhaj Haider - avatar
0
In first There's 2 row and at Every row 3 column But in second there's 2 row but no column so There's like this {{null,null},{null,null}}; ?
24th Jan 2022, 2:18 PM
Minhaj Haider
Minhaj Haider - avatar
0
aa is internally first array of Objects int[], if there are not elements, default value is null not empty objects Object[] b = new Object[2]; System.out.println( Arrays.toString(b)); // [null, null] // compare to this Object[] d = new Object[2]; Object c1 = new int[3]; Object c2 = new int[3]; d[0] = c1; d[1] = c2; System.out.println( Arrays.deepToString(d)); // [[0, 0, 0], [0, 0, 0]]
24th Jan 2022, 4:36 PM
zemiak
0
> ".. in second there's 2 row but no column" by [2][] length of "column" is not specified, but you expect length 2 {{null,null},{null,null}}; // wrong row = {{column1},{column2}};
24th Jan 2022, 7:53 PM
zemiak
0
Why not 0 earlier there's 0 here's null how can I know where's 0 or where's null...
25th Jan 2022, 10:13 AM
Minhaj Haider
Minhaj Haider - avatar
0
for int is default 0 for Object is default null array int[] is internally Object
25th Jan 2022, 10:43 AM
zemiak
0
Then what's the difference in example num 2) and no 3) both have new and int keyword then num 2) keyword have 0 and num 3) have null....
25th Jan 2022, 12:06 PM
Minhaj Haider
Minhaj Haider - avatar
0
you need understand it is not simple one 2D data type, like matrix(x,y) with same elements. this implementation is complex of 1D arrays where one has different type of elements
25th Jan 2022, 12:52 PM
zemiak
0
Ok, thank you
25th Jan 2022, 1:47 PM
Minhaj Haider
Minhaj Haider - avatar