tv controls
tv logo
tv power
tv channel
tv brightness
tv color
tv top
blog logo
tv shell top
tv black screen

Harley the Cat’s Coding Corner: NullPointer Exceptions

Harley, The Feline Princess

Harley, Hard at Work

Lately it seems that it’s all the rage to be a Barista.  And since I’m a Java programmer, I figure that’s close enough.

According to the Fivebucks Overpriced Hot Chocolate Company (editor’s note: that’s Starbucks Coffee Company) their baristas “create an uplifting experience for our customers and make perfect drinks – one drink and one customer at a time”.  As they always provide a perfect flavor profile for my hazelnut hot chocolate,  would tend to agree with this statement.

“What does this have to do with programming”, you ask?

“Excellent question!” I respond.  “I am well-equipped to create an uplifting experience for my readers and make perfect Java (code) – one line and one function at a time”.  (I told you it was a stretch, work with me here people!).

So, now…  I introduce you to a new feature here on the Blogmaster2k: Harley’s Coding Corner.  Harley, pictured above, is our family cat.  She performs many functions in our household from catching mice to unfettered purring and showing unconditional love.  (Curtis’ note: she really doesn’t do any of those things, at least for me.)

In addition to providing all of the above (for the other members of the family) Harley also serves as my Code and Article editor.  In this week’s Cat Chat she suggested that I extol the virtues of clean exception management in Java code.

She reminded me of a long  time ago, in a galaxy near, near to here when I was debugging some tricky Java code.  I was getting an exception (I don’t recall which) and needed to check the data values of a variable to see what portion of the object was incorrect.  I ended up with something a bit like this:

try {
  CurtsWonderObject obj = DAO.getCurtsWonderObject(parameters);

  obj.setProperty1(property);
  obj.setProperty2(property);
  obj.setProperty2(property);

  obj.doSomeStuff();

  DAO.saveCurtsWonderObject(obj);

} catch (Exception e) {
  log.info("CurtsWonderObject: --debuginfo--");
  log.info("  obj.property1: " + obj.getProperty1());
  log.info("  obj.property2: " + obj.getProperty2());
  log.info("  obj.property3: " + obj.getProperty3());
} finally {
  ...
}

This is obviously brilliant psuedocode. Ok, it won’t win any sort of programming award, nor will it probably even compile and work; but that’s not what this Code Review is about, is it? (editor’s note: no, it’s not).  Now, this should have given some highly-useful information such as the actual data values of said wonder object.  However, upon execution, what I did get was an entirely unhelpful NullPointer exception in the function which was calling this function of which the debug portion was intricately crafted to give me myriad information about what was going wrong.

:: sigh ::

After scratching my head a bit, I woke Harley up (she was happily napping on the Comcast Digital Cable box which seems to be quite warm due to the filtering ducts being clogged with shed cat hair).  With blinking eyes and a why in the world did you wake me up this early for code review look upon her mug, she took over the mouse and keyboard to look for code.

After tracing the NullPointer exception she encountered the fact that it was being thrown from my freaking exception handling code!  After many a kitty-guffaw, she wrapped my exception handling code in an if (ojb != null); block.  Unfortunately, I had to erase the smug look from her face with a can of gratitude.

Moral:  ALWAYS, I repeat ALWAYS wrap any code you think might be problematic at runtime in valid “if null” blocks.  You will thank yourself later.  And you won’t have to wake up your editor (she recently repaid me for the incident by plopping on my chest and waking me with her tail on a lazy Saturday morning.)

P.S.  For those that aren’t familiar with a NullPointer exception (or an Exception) for that matter.  It’s the generally-accepted best way for an Object Oriented language to provide you information for a run-time error occurring in your program.  In effect, it means that you, as a programmer, have been a humongous doofus when coding a part of your program.  You’ve neglected to create or setup an object properly in a way the computer can utilize it.

It would be somewhat akin to telling your child to clean his room, and then getting told by your spouse that your child slept over at his friends house and isn’t there to clean the room.  Your spouse just threw a NullPointer exception at you when you tried to reference your non-present child.

Edit: Several readers have suggested posting the modified code showing the final version. Agreeing that it was a stupendous idea, I complied.

try {
  CurtsWonderObject obj = DAO.getCurtsWonderObject(parameters);

  obj.setProperty1(property);
  obj.setProperty2(property);
  obj.setProperty2(property);

  obj.doSomeStuff();

  DAO.saveCurtsWonderObject(obj);

} catch (Exception e) {
  if (obj != null) {
    log.info("CurtsWonderObject: --debuginfo--");
    log.info("  obj.property1: " + obj.getProperty1());
    log.info("  obj.property2: " + obj.getProperty2());
    log.info("  obj.property3: " + obj.getProperty3());
  }
} finally {
  ...
}

One Comment

  1. avatar Snakesbeard
    Posted November 18, 2010 at 6:04 pm | Permalink

    Sounds like you need a new CAT program. try this… thishttp://www.bitboost.com/pawsense/

    Or maybe something like this…
    lines.http://www.moggies.co.uk/html/quake.html

    🙂

Post a Comment

You must be logged in to post a comment.

tv copy bottom
SEARCH
spacer
AUTHORS
tv shell bottom
tv people
tv shell bottom