This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "NullPointerException" Watch "NullPointerException" New topic
Author

NullPointerException

Shweta R Dhaneshwar
Ranch Hand

Joined: May 27, 2005
Posts: 35
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?


Shweta Dhaneshwar.<br />SCJP 1.4 90%
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
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 ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
deshdeep divakar
Ranch Hand

Joined: Apr 19, 2004
Posts: 91
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...


"Do not be afraid of going slow, be afraid of standing still"
deshdeep divakar
Ranch Hand

Joined: Apr 19, 2004
Posts: 91
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

Joined: Sep 28, 2004
Posts: 1228
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 ]
Devender Thareja
Ranch Hand

Joined: Jul 14, 2005
Posts: 187
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 ]

Devender Thareja
SCEA, SCBCD, SCJP
Fes D Gaur
Ranch Hand

Joined: Apr 29, 2005
Posts: 54
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
Veer Batra
Ranch Hand

Joined: Mar 12, 2001
Posts: 35


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

Joined: Sep 28, 2004
Posts: 1228
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

Joined: Jul 14, 2005
Posts: 187
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointerException
 
Similar Threads
Doubt in Arrays(Self Test Question)
Object Array doubt,please help
scjp exam problem
scjp help required
Sierra Bates SCJP 6 Ch. 3 No. 3