Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1115 :
Posted: Sun Mar 03, 2013 1:48 am
by Sweetpin2
Why int ia1[] = {null}; throws compile error (Type mismatch: cannot convert from null to int). But below does not throw error
int ia2[][] = { {1, 2}, null };
Re: About Question enthuware.ocajp.i.v7.2.1115 :
Posted: Sun Mar 03, 2013 6:47 am
by admin
Because in case of ia[], each element of the array must be an int. A null cannot be converted into an int.
But int case of ia2[][], each element of ia2[] is an array (of ints) and since an array is an object, a null is valid value for an object.
Re: About Question enthuware.ocajp.i.v7.2.1115 :
Posted: Sun Sep 20, 2015 8:19 pm
by ReedClowen
How come
Code: Select all
int [][] a = {{1},null}
System.out.print(a[1][0]);
will give you a NullPointerException but
Code: Select all
int [][] a = {{1},null}
System.out.print(a[1]);
will simply print out "null."
Re: About Question enthuware.ocajp.i.v7.2.1115 :
Posted: Sun Sep 20, 2015 8:32 pm
by admin
Because in the first case, a[1] is null. So when you try to access a[1][0], you are trying to access an element in null. This will cause a NPE.
In the second case, you are not calling anything on null. You are just passing a[1] (which is null) to the print method, which checks if the argument is null, and if it is, prints null.
Re: About Question enthuware.ocajp.i.v7.2.1115 :
Posted: Thu May 19, 2016 12:57 pm
by Ingvarr
I wonder why the explanation talks about accessing the 3rd element of the main, top-level array (quoting: "If you try to access ia[2][0], it would have thrown ArrayIndexOutOfBoundsException because the length of ia is only 2 and so ia[2] tries to access an element out of that range. ia[2] is not null, it simply does not exist.")
The explanation per se is absolutely correct; I just don't think it's much relevant: after all, neither i nor j in the code will ever become 2. On the other hand, if ia[1][0] were some int instead of a null, an attempt to access this slot would have also thrown an AIOOBE...
Re: About Question enthuware.ocajp.i.v7.2.1115 :
Posted: Thu May 19, 2016 8:28 pm
by admin
It is just giving the reader additional insight by contrasting ia[1] and ia[2]. One is null and one does not exist.
Re: About Question enthuware.ocajp.i.v7.2.1115 :
Posted: Mon Feb 20, 2017 12:47 pm
by Javier
Hi Admin!!
One person asked one question before about this piece of code:
public static void main (String[] args) {
FunWithArgs fwa = new FunWithArgs();
String[][] newargs = {args};
"Is this not trying to assign a 1d array to a 2d array?"
and the Admin answer was:
"No, it is not, notice the {}. You would be right if it were:
String[][] newargs= args; // wich of course wouldn´t compile"
My question is: What is exactly doing "String [][] newargs = {args};" in that snippet code? Thank you so much.
( I post here this question because I can not find where was posted this question, so sorry about that)
Re: About Question enthuware.ocajp.i.v7.2.1115 :
Posted: Mon Feb 20, 2017 9:46 pm
by admin
{ } is a syntax for defining an array. For example, { 1 } is an array of ints with only one int element. Now, args is an array of Strings. So { args } is an array of string arrays with only one element.
Re: About Question enthuware.ocajp.i.v7.2.1115 :
Posted: Sat Oct 20, 2018 7:19 am
by oumayma
Please why when using
String s = null;
System.out.println(s);
prints NULL
but with array
int ia[][] = { {1, 2}, null };
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
System.out.println(ia[j]); It will throw a NullPointerException for ia[1][0] because ia[1] is null.
We try to access the item and print it !!!
thank you
Re: About Question enthuware.ocajp.i.v7.2.1115 :
Posted: Sat Oct 20, 2018 10:08 am
by admin
oumayma, your question is already answered in admin's reply above.
You are just passing a[1] (which is null) to the print method, which checks if the argument is null, and if it is, prints null.
The print/println methods don't invoke toString method on the argument if the argument is null. They just print the string null in such a case.
Re: About Question enthuware.ocajp.i.v7.2.1115 :
Posted: Sun Apr 06, 2025 8:45 am
by n199a_
Question Summary
Code: Select all
class ArrayTest { public static void main(String[] args) { var ia[][] = { {1, 2}, null }; for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) System.out.println(ia[i][j]); } }
Provided Answer Choices
It will throw an
ArrayIndexOutOfBoundsException at runtime.
It will throw a
NullPointerException at runtime.
It will compile and run without throwing any exceptions.
It will compile and throw a
NullPointerException at runtime if
is replaced with
Code: Select all
var ia = new int[][]{{1, 2}, null};
Final Answer
None of the answer choices are correct.
Both the original code and the suggested replacement do not compile.
This is a compilation-time error, not a runtime exception.
Re: About Question enthuware.ocajp.i.v7.2.1115 :
Posted: Sun Apr 06, 2025 4:52 pm
by admin
The correct option as shown by the simulator is option 3, "It will throw a NullPointerException at Runtime.".
If you are seeing something else, please post a screenshot.