 Why 'null' and NullPointerException is occurring? | Sololearn: Learn to code for FREE!

+1

# Why 'null' and NullPointerException is occurring?

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

+4

import java.util.Arrays; public class Program { public static void main(String[] args) { // int[][] a = new int; // 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[]; // is shortcut to int[][] aa = {null, null}; System.out.println( Arrays.deepToString(aa)); } }

+1

3rd example creates a[null, null] null throws Exception

+1

0

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}}; ?

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; System.out.println( Arrays.toString(b)); // [null, null] // compare to this Object[] d = new Object; Object c1 = new int; Object c2 = new int; d = c1; d = c2; System.out.println( Arrays.deepToString(d)); // [[0, 0, 0], [0, 0, 0]]

0

> ".. in second there's 2 row but no column" by [] length of "column" is not specified, but you expect length 2 {{null,null},{null,null}}; // wrong row = {{column1},{column2}};

0

Why not 0 earlier there's 0 here's null how can I know where's 0 or where's null...

0

for int is default 0 for Object is default null array int[] is internally Object

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....

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