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

Is there a race condition in this code?

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

Is there a chance of me getting a NullPointerException between the lines
8 to 11 ? This might be a stupid question. Bear with me. Thanks
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.
 
Pho Tek
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I can sleep well at night. Thanks Jim.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The initialisation of "handler" to null is unnecessary.

Generally, one should not initialise variables except when the algorithm really requires it. Unnecessary initialisations: -

  • bloat the code
  • defeat the useful feature of the compiler that checks whether variables are initialised before they are used

  •  
    Pho Tek
    Ranch Hand
    Posts: 782
    Python Chrome Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Peter, good point!
     
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You can get NullPointerExecption at line 5 if m_handlerRef is null
     
    Jim Yingst
    Wanderer
    Posts: 18671
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yes, but since m_handlerRef is final, the compiler must have forced the programmer to initialize the variable in a constructor or something, not shown here. It's possible that for some reason it's been initialized to null, but that's hardly a race condition - it's a fundamental problem with the program that will be very obvious as soon as it appears. Race conditions are generally much more elusive.
     
    Pho Tek
    Ranch Hand
    Posts: 782
    Python Chrome Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    @Gladwin,

    m_handlerRef is assigned a value in the constructor for the class.
    So it can't ever be null.
    [ March 10, 2008: Message edited by: Pho Tek ]
     
    I don't even know how to spell CIA. But this tiny ad does:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic