Sunday, February 11, 2007

NPEs and Static Analysis
Some days ago I was happily coding away when I stumbled over a bug in some Java code that I was using. More specifically I was getting a so called Null Pointer Exception, which is fairly common problem type. The funny thing this time was how long I had to stare at the one line of code before I saw the problem. The line contained several potential problems, but even a simplified example might look non-obvious...

  public void printWinner(String name) {
    System.out.println("The winner is: "
      + name != null ? name : "n/a");
  }


Do you see the problem? A quick code search shows that this bug exists elsewhere too. Now, if it's that easy to find a bunch of bugs like this, shouldn't there be a whole class of problems that can be detected automatically? Of course! Check out the Google engEdu tech talk on Static Analysis.

No comments: