aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes array 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 "array" Watch "array" New topic
Author

array

sreelakshmi sarma
Ranch Hand

Joined: Mar 02, 2000
Posts: 130
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

Joined: Jan 30, 2000
Posts: 3222
    
    5
I'm leaving that one to someone else. It doesn't make sense. I wonder what JDK 1.3 beta does...


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.

"I'm not back." - Bill Harding, Twister
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
Thanks Jim. I knew you would come up with he answer
Arijit Ghosh
Ranch Hand

Joined: Feb 01, 2002
Posts: 174
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 ??


Regards,<br /> Arijit
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Jim:
Does 1.4 compiler also exibit same behavior? I do not have it yet...
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
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.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
 
wood burning stoves
 
subject: array