Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ambiguity in S&B book

 
Johnny Hunter
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Johnny Hunter
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic