Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array

 
sreelakshmi sarma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
When i tried the following code i getting NullPointerException.
code:
class testarr {

public static void main(String[] args) {
char c[][]=new char[10][];
System.out.println(c[5]);
}
}
But when i changed the datatype to any other datatype like int,byte,long even String i am not getting any error. It's simply printing "null". Why is that?
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm leaving that one to someone else. It doesn't make sense. I wonder what JDK 1.3 beta does...
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jdk 1.3 beta does the exact same thing. The reason is that if you look at the API for PrintStream (which is what System.out is an instance of), you see:
<code><pre> public void println(char[] s)
public void println(Object obj)</pre></code>
There's no overload for any type of array besides char[] - any other array ends up invoking println(Object) instead. And it just happens that the authors of println(Object) thought to handle a NullPointerException by catching it themselves and printing the string "null", whereas the authors of println(char[]) didn't bother to catch a NullPointerException, though they probably should have.
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim. I knew you would come up with he answer
 
Arijit Ghosh
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was going through old posts, and I found this interesting... I am having a similar doubt...

Following is the output --
a[0]->null
b[0]->null
Exception in thread "main" java.lang.NullPointerException
at abc.main(abc.java:7)

Any explanation ??
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim:
Does 1.4 compiler also exibit same behavior? I do not have it yet...
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java 1.4 gives same NullPointerException.
What we are discussing here is the behaviour
of System.out.println when it gets a null pointer to char[], right?
c[5] == null is correct.
So when we try to get at whatever c[5] is supposed to be referencing (a char[]) we are going through a null reference and so we get the NullPointerException.
Therefore I would say the behaviour is as expected.
-Barry
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If b[0] is null (which it is), then any reference to b[0][0] is going to cause a NullPointerException. That happens before System.out.println even gets called.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic