• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

array

 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jim. I knew you would come up with he answer
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ??
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jim:
Does 1.4 compiler also exibit same behavior? I do not have it yet...
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic