• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Assignment a problem.

 
Greenhorn
Posts: 16
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
K&B book chapter assignment,question no-4



ans- hi hi following exception on line 16 how?
and it should also throw same null pointer exception on line 14 why it is not thrown.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you think it should throw an exception on line 14?

Just follow the code:
10. m2 = object, m2.m1 = null
11. m3 = object, m3.m1 = m2 (object)
13. m4 = m3.m1, which is the same as m2. So it isn't null.
15. m5 = m2.m1, which is null

So calling m4.go() is fine, but calling m5.go() isn't.
 
Gaurav Gulanjkar
Greenhorn
Posts: 16
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello sir,
can you help me with tracing the output line by line.
thank you.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what my post was intended to do. It shows what gets assigned on each line, so you can see which values are null and which aren't.

If you think an exception should be thrown on line 14, that would suggest you think m4 is null. But it isn't null, as my annotation to line 13 shows.

Whereas on line 16, m5 is null, so an exception is thrown when you try to call a method on it.
 
Greenhorn
Posts: 8
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please follow standard JavaBeans naming conventions: Classes start with a Capital letter.

So, NOT

BUT
 
Brace yourself while corporate america tries to sell us its things. Some day they will chill and use tiny ads.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic