Forums Register Login

Multidimention array QUestion

+Pie Number of slices to send: Send
Hi All,
i have question on multidimensional array following is code :


on line i am expecting address of null or NullPointerException but it prints null.

My understanding is as follows :

why i think there could be NulPointerException
Rule i know : "when we access or Modify the slots of null as if it were an array "--NullPointerException is thrown

Please clear my doubt.
NULL always confused me also please suggest where can i get facts about NULL which are important for Exam as well as java programmer.
+Pie Number of slices to send: Send

about this:

Array is object. Multidimensional array is object where each element is object of type one dimensional array.
every object reference you may assigned null

if you try after that acces

you will get NullPointerException
because it is like


every element of


names [0] - is object type String[]
names [1] - is object type String[]
names [2] - is object type String[]
names [3] - is object type String[]
and to it you may assigned null

and one more


1
+Pie Number of slices to send: Send
and little more code:
when you alocate array like this

you will get
strA[0] = null;
strA[1] = null;
because when you alocate array each element get it default value (for object type in this case - String[] this is null)

NullPointerException you will get
1. if you try access alement of array or ArrayList if it is null.
2. use method invocation or variable on refference that value is null.
remark: instance and static variable of reference type if not assigned value get null. If you explicit assigned null value it will have null too.

also note that local refference variable not get default value even null - if you use it before asigment you code not compile , and final instance and final static variable must be intialized or you get compiler error.
more from jls about array 15.13.1. Run-Time Evaluation of Array Access


An array access expression is evaluated using the following procedure:
First, the array reference expression is evaluated. If this evaluation completes abruptly, then the array access completes abruptly for the same reason and the index expression is not evaluated.
Otherwise, the index expression is evaluated. If this evaluation completes abruptly, then the array access completes abruptly for the same reason.
Otherwise, if the value of the array reference expression is null, then a NullPointerException is thrown.
Otherwise, the value of the array reference expression indeed refers to an array. If the value of the index expression is less than zero, or greater than or equal to the array's length, then an ArrayIndexOutOfBoundsException is thrown.
Otherwise, the result of the array access is the variable of type T, within the array, selected by the value of the index expression.



1
+Pie Number of slices to send: Send
also cool example:

and
2
+Pie Number of slices to send: Send
 

aditya chaudhari wrote:System.out.println(names[0][3]); //ptints address of {"AAA","BBB"} array
System.out.println(names[2]); //prints address of {"CCC"} array


That's not true at all! Any array (int[], String[], Object[][],...) is a subclass of java.lang.Object. And arrays do not override the toString() method, so it is inherited from the java.lang.Object class. The toString() method of the java.lang.Object class prints the object type, followed by an @, followed by the identity hashcode in hex. So it has nothing to do with the address.

aditya chaudhari wrote:System.out.println(names[1]); //prints null...i am expecting address of null or NullPointerException here where am i missing....???


I have a simple question for you: what's the difference between these 2 statements: System.out.println(names[3][0]); and System.out.println(names[1]);? In both statements you refer to an element which is null (not initialized). In the 1st statement a String, in the 2nd one a String[]. Both elements are null (not initialized) and are printed. So both statements perform exactly the same thing (printing null). Why would the outcome of the 2nd statement be different from the 1st one?

Hope it helps!
+Pie Number of slices to send: Send
Hi Roel and Sergej ,

Sergej ,
You have given useful codes .
your following sentence helps me to understand :

remark: instance and static variable of reference type if not assigned value get null.
If you explicit assigned null value it will have null too.
The example below above line is very informative.


Roel ,
as you metioned :
...In both statements you refer to an element which is null (not initialized).

we can say that here for this example -- "null means the String Object was not instantiated".

i aologize for Typo of below line i was experimenting and by mistake put // comments. part. please ignore below line
System.out.println(names[0][3]); //ptints address of {"AAA","BBB"} array.

Roel i understand (Please corrrect if i am wrong ):

System.out.println(names[3][0]);-- Refers to null at position 0 of String array names at position 3.
System.out.println(names[1]); String array at position 1 refers to NULL.
1
+Pie Number of slices to send: Send
 

aditya chaudhari wrote:we can say that here for this example -- "null means the String Object was not instantiated".


Not 100% true.

In the example System.out.println(names[3][0]); the String object is indeed not initialized. But in the example System.out.println(names[1]); it's a String array (NOT a String object) which is not initialized.

aditya chaudhari wrote:System.out.println(names[3][0]);-- Refers to null at position 0 of String array names at position 3.
System.out.println(names[1]); String array at position 1 refers to NULL.


True!

Just a minor remark: names[3][0] refers to the element at position 0 of the String array at position 3 of the 2-dimensional array names.

Hope it helps!
That feels good. Thanks. Here's a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 649 times.
Similar Threads
Duplicate variables and initialization blocks
What is the input file?
Problem Retrieving String with new line.
beginner
cast argument '0' to 'short' error
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 05:13:04.