aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Ambiguity in S&B book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Ambiguity in S&B book" Watch "Ambiguity in S&B book" New topic
Author

Ambiguity in S&B book

Johnny Hunter
Ranch Hand

Joined: Jul 15, 2004
Posts: 37
Page 44 it reads:
You'll get a NullPointerException if you try to use an array element in an object array, if that element doesn't refer to a real object. ???


public class Test {
public static void main(String argv[]){
Object[] obj = new Object[2];
obj[0] = null;
System.out.println(obj[0] + ", " + obj[1]);
}
}


or does it have to do with obj[][]? Curious.


Johnny Hunter
SCJP 1.4
Johnny Hunter
Ranch Hand

Joined: Jul 15, 2004
Posts: 37
OK> Iget a NullPointerException with that code:
public class Test {
public static void main(String argv[]){
Object[][] obj = new Object[2][];
obj[0] = new Object[1];
System.out.println(obj[0][0] + ", " + obj[1][0]);
}
}

Can someone confirm that the text in S&B describes this situation? Thanks!
Tybon Wu
Ranch Hand

Joined: Jun 18, 2002
Posts: 84
Both of those code samples are demonstrating the same thing. They are both trying to access an element which has null reference in an object array.


SCJP2
Johnny Hunter
Ranch Hand

Joined: Jul 15, 2004
Posts: 37
Yes, except that the second listing throws an NullPointerException and the first doesn't (it just prints null, null)

I just want to make sure that I understand what the sentence is stating. It's not that clear and doesn't even seems written in proper English.
vijaya reddy
Greenhorn

Joined: Jul 21, 2004
Posts: 1
hi

in ur 2nd eample for object obj

Object[][] obj = new Object[2][];
u haven't initialized it with second array index of second array.obj[1]=new Object[1];

thats why you are getting NullPointerException when u r trying to output the value of obj[1][0].

if u do like

obj[1]=new Object[1];
then u will get out put as null.
hope this will help u.
iam not good in explining but try to understand.
Tybon Wu
Ranch Hand

Joined: Jun 18, 2002
Posts: 84
Ok, I think the problem is that the author chose a bad code example. Since System.out.println(Object obj) will output "null" if obj is a null reference, the element of the object array was never actually used directly. Instead, the author should have demonstrated the word "use" by calling a method of the Object type on the array element, for example, obj[0].toString(), which would have caused the NullPointerException to be thrown, as intended.

For the second Test example, NullPointerException was thrown because there was an attempt to reference an array which has not been created, which is possible with multidimentional arrays with incomplete array creation expressions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ambiguity in S&B book