• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing "null" array elements

 
Suhasini Na
Greenhorn
Posts: 18
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am preparing to take the OCAJP exam next month and am currently going through the Enthuware mock exams. I have the following question regarding accessing "null" array elements
Here is code from question 35 in Test 2


The above code compiles successfully and prints "null". We are just printing out the array element whose value is null.
Is it because a String array can take "null" as valid values and it can be printed as value?

However, there is another question 10 in Test 4


The above code throws a "NullPointerException". Now this code should also print null as ia[1][0] is null. But since ia[1] is null, we cannot access ia[1][0] and ia[1][1] values. It throws a NullPointerException.

Am I thinking right? Please let me know.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are thinking correctly. Accessing a null reference (i.e. trying to call a method on it or trying to access a member field) always causes the JVM to throw a NullPointerException.
The print method simply checks the input for null before invoking toString on it. If the input is null, it prints "null". That is why there is no NPE when you try to print null but there is a NPE as soon as you try to apply the indexing operator[] on it.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just noticed one thing in your post that is fundamentally incorrect.
Suhasini Na wrote:
Now this code should also print null as ia[1][0] is null.


ia[1][0] is not null. ia[1] is null. Remember that the array referred to by ia stores references. i.e. each element of the array referred to by ia must either be a valid reference or be null.
ia[0] does store a valid reference, which points to another array object, while ia[1] is null. A null implies that ia[1] is not pointing to anything. Since ia[1] is not pointing to anything, saying that ia[1][0] is null is wrong. Had ia[1][0] been null, System.out.println(ia[1][0]) would have printed null. But it does not. It throws a NPE because of the reason mentioned above.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Suhasini Na,

First of all, a warm welcome to CodeRanch!

Suhasini Na wrote:The above code compiles successfully and prints "null". We are just printing out the array element whose value is null.
Is it because a String array can take "null" as valid values and it can be printed as value?

You are spot-on! null is a valid value for a (String) reference variable (it's an invalid value for a primitive variable). And you can print such a reference variable without getting a NullPointerException. But if you invoke an instance method on a null reference variable, you'll get a NullPointerException.

Suhasini Na wrote:The above code throws a "NullPointerException". Now this code should also print null as ia[1][0] is null. But since ia[1] is null, we cannot access ia[1][0] and ia[1][1] values. It throws a NullPointerException.

As you (probably) know, each array IS-A Object. That means that null is also a valid value for a reference variable to an array. And because ia is a 2D-array, each element of this array is a reference variable to a 1D-array of int. So null is definitely a valid value. It actually means that ia[1] is not refering to a 1D-array of int. You could print the value of ia[1] (and you'll get null as in the previous example), but if you try to access an element of this non-existing array (e.g. ia[1][0]), you'll get a NullPointerException.

Now let's see if you have understood everything. Here's a little pop quiz. What's the result of these code snippets?

Hope it helps!
Kind regards,
Roel
 
Suhasini Na
Greenhorn
Posts: 18
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel De Nijs,
Roel De Nijs wrote:First of all, a warm welcome to CodeRanch!

Thank you. After having read so many of your great answers to different posts, I was excited to get your warm welcome.
Roel De Nijs wrote:You could print the value of ia[1] (and you'll get null as in the previous example), but if you try to access an element of this non-existing array (e.g. ia[1][0]), you'll get a NullPointerException.

I tried the above line and that made it crystal clear for me. You understood my thought process without me asking it right..
Roel De Nijs wrote:Now let's see if you have understood everything. Here's a little pop quiz. What's the result of these code snippets?

Answer: This throws a ArrayIndexOutofBoundsException because a[3][1] exists. However its bounds are only upto a[3][1]. So we cannot access arr[3][1][0]. We can access arr[3][1] and it prints a hashcode of the empty array
Roel De Nijs wrote:

This is existing array element {"y"}. Hence I can perform toUpperCase operation on it. It does return the result "Y"
Roel De Nijs wrote:

Throws a null Pointer exception. For the reason that it is a non-existing array. We cannot perform any index based operation on non-existent array elements indexes */
Roel De Nijs wrote:

Here 1-D array of int cannot take null values. Its an invalid value for primitive int-array. Hence prints "error null cannot be converted to int"
Roel De Nijs wrote:

Here its an Integer object array. {6, null, 8} is a 1-D Integer-object array which can take null as valid values. It can thus print "null" for arr[2][1]
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You gave for every example the correct answer! But not every explanation was 100% accurate and correct. That's why I comment on your explanations if needed. Here we go!

Suhasini Na wrote:Answer: This throws a ArrayIndexOutofBoundsException because a[3][1] exists. However its bounds are only upto a[3][1]. So we cannot access arr[3][1][0]. We can access arr[3][1] and it prints a hashcode of the empty array

Indeed, arr[3][1] is an empty array (if you print arr[3][1].length, you'll get 0). So this array doesn't have any elements. So trying to access the 1st element (using arr[3][1][0]) will result in an ArrayIndexOutOfBoundsException.

Suhasini Na wrote:This is existing array element {"y"}. Hence I can perform toUpperCase operation on it. It does return the result "Y"

You are absolutely spot-on!

Suhasini Na wrote:Throws a null Pointer exception. For the reason that it is a non-existing array. We cannot perform any index based operation on non-existent array elements indexes

arr is a 3D-array of String reference variables => arr[0] is a reference variable to a 2D-array of String reference variables => arr[0][1] is a reference variable to a 1D-array of String reference variables => arr[0][1][2] is a String reference variable (and not an array). So because arr[0][1][2] is a String reference value with a null value, invoking an instance method (like toString()) will result in a NullPointerException.

Suhasini Na wrote:Here 1-D array of int cannot take null values. Its an invalid value for primitive int-array. Hence prints "error null cannot be converted to int"

This code will not compile! Because as you stated correctly: null is not a valid value for the primitive data type int. But that's a compiler error, so because you can't compile the code successfully, you can't execute the code (and thus nothing will be printed).

Suhasini Na wrote:Here its an Integer object array. {6, null, 8} is a 1-D Integer-object array which can take null as valid values. It can thus print "null" for arr[2][1]

To be 100% correct, arr is a 2D-array of Integer reference variables (and not Integer objects). But that's just a very minor remark, the rest of your explanation is correct.

Hope it helps!
Kind regards,
Roel

PS. I awarded you a cow, because you gave the correct answer for every example.
 
Suhasini Na
Greenhorn
Posts: 18
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote: PS. I awarded you a cow, because you gave the correct answer for every example.


Thank You.. I got it now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic