• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

NullPointerException

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please help me clear this doubt.Check out the code and its o/p.

here the correct ans is option D.

one more...

for this the correct option is B.

consider this...

here o/p is null.

I am a little confused...when do we get o/p null and when do we get NullPointerException?
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check this piece of code

Try to run it and surely you 'll get to know ( This a better way to learn ). If you face any problem again , please update this thread.
[ July 28, 2005: Message edited by: Srinivasa Raghavan ]
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Buddy
Srinivasa Raghavan
When I assigned String x = "null";
the
Output is :null null does this mean that in case when
we say String x="null"; reference variable x value
does'nt have any spaces while saying String x=null;
does have space.Plz elaborate on this...
 
deshdeep divakar
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Buddy
Srinivasa Raghavan
When I assigned String x = "null";
the
Output is :null null does this mean that in case when
we say String x="null"; reference variable x value
does'nt have any spaces while saying String x=null;
does have space.Plz elaborate on this...
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry if the code mis leaded you.

I ment to say you 'll get a null pointer exception if you are trying to access any method in an object which is null.

[ July 28, 2005: Message edited by: Srinivasa Raghavan ]
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you try to print a null object reference, it will print "null".
If you try to invoke a method on a Object reference which is null, it will throw null poiner exception. Same will happen if you try to access a field from null object reference.
[ July 28, 2005: Message edited by: Devender Thareja ]
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the first question:
When arrays are declared whether at class level or local (method) level, their elements are given default values. In this case the array object is being declared as type Dog. The default values in the array object is null. When you call toString() or trim() on a reference that is not pointing to an object a NullPointer Exception is generated at run time.

For the second question: since the array values are by default null and since you are passing only two arguments the array element at index 2 will be null.

Last question simply is printing null because that reference is null.

Hope this helps.

Fes
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If line 4 is there then it gives nullPointerException on line 4. If line 4 commented out then it gives same exception on line 5. Line 4 which is not invoking any method, why it should give nullPointerException on that. Should it not print null and give exception on line 5 only?
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Veer,

Line 4 which is not invoking any method


Are you sure ?

Think what will happen when the above line gets executed .. & whant gets printed & from where it came ?
[ July 29, 2005: Message edited by: Srinivasa Raghavan ]
 
Devender Thareja
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Veer Batra:


If line 4 is there then it gives nullPointerException on line 4. If line 4 commented out then it gives same exception on line 5. Line 4 which is not invoking any method, why it should give nullPointerException on that. Should it not print null and give exception on line 5 only?



<code>
Dog [][] theDogs = new Dog[3][]; //line 3
</code>

Here you are telling compiler that Dog is an array of 3 arrays. You haven't initialized those 3 arrays yet. Once you create those arrays, then only it will initialize it with null reference of Dog object.

Try replacing line 3 with following and see what happens:
<code>
Dog [][] theDogs = new Dog[3][3]; //line 3
</code>

Now your line 4 should print null.
 
reply
    Bookmark Topic Watch Topic
  • New Topic