• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why the program hangs up at runtime

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code in from exercise of K&B . The answer shows that it might throw Nullpointer exception as well as program might hang without ever completing.

I don't understand why it will throw Null Pointer Exception. All though answer explains since Integer i is not initialized it.. But according to my knowledge instance variables are assigned value '0' .

Here's the code:



Kindly anyone explain this to me..
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Divyya Joshi wrote:But according to my knowledge instance variables are assigned value '0' .



This must be today's catch-phrase: "According To My Knowledge". I seem to be spending my whole morning pointing out that people's "knowledge" is incorrect.

And yes, it's incorrect. Instance variables are only assigned value '0' if they are of type char and the programmer explicitly assigns that value.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul might be slightly pickily distinguishing between '0' and 0 (), but he's right that this is not what happens. Reference member variables get initialised to null if a value isn't specified, and Integer is a reference variable.
 
Divyya Joshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul and Matthew:)
Is it that the program would have run fine if instead of Integer we had int as instance variable?
 
Ranch Hand
Posts: 40
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is it that the program would have run fine if instead of Integer we had int as instance variable?



yes! it runs fine if you use int because instance variables of type "int" are given 0 as their default value.
 
Ranch Hand
Posts: 176
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What might cause this program to hang?

Is it that Case 0: might never get invoked?

Is there any other reason?
 
Glen Iris
Ranch Hand
Posts: 176
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Glen Iris wrote:What might cause this program to hang?

Is it that Case 0: might never get invoked?

Is there any other reason ?

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic