Monday, September 25, 2006

A 10 year old null pointer bug

I was doing some historical analysis of FindBugs warnings in the JDK, and found that there is one null pointer warning that FindBugs generates on the 1.0.2 version of Sun's JDK that still exists in JDK 1.6.0-b99.
The bug is that if you call setHelpMenu(null) on a java.awt.MenuBar, and there is already an existing help menu, you'll get a null pointer exception. The buggy code is:

/**
* Sets the specified menu to be this menu bar's help menu.
* If this menu bar has an existing help menu, the old help menu is
* removed from the menu bar, and replaced with the specified menu.
* @param m the menu to be set as the help menu
*/
public void setHelpMenu(Menu m) {
synchronized (getTreeLock()) {
if (helpMenu == m) {
return;
}
if (helpMenu != null) {
remove(helpMenu);
}
if (m.parent != this) {
add(m);
}
helpMenu = m;
if (m != null) {
m.isHelpMenu = true;
m.parent = this;
MenuBarPeer peer = (MenuBarPeer)this.peer;
if (peer != null) {
if (m.peer == null) {
m.addNotify();
}
peer.addHelpMenu(m);
}
}
}
}

Now, the JavaDoc doesn't say that you should call setHelpMenu(null) to remove a help menu. But there is no other way to remove a help menu, and the check for if (m != null) suggests that the developer anticipated m being null. But we know that m can't be null here, because if m were null, a null pointer exception would have occurred at if (m.parent != this)

Why hasn't this been noticed or fixed in the past 10 years? I don't know, but I've now filed a bug report.

28 comments:

  1. With NPE being one of the most common type of exceptions thrown (probably next to CCE and AIOOBE), it almost makes sense to make a non-nullable type (or make null a type). As C# has created nullable types, what about the reverse for Java?

    ReplyDelete
  2. This is a very cool tool to improve the code quality.

    The best parts i like are NPE, Infinite Loops & Duplications. I was amazed to see some of the issues reported by this.

    Thanks william for coming up with such a nice tool.

    ReplyDelete
  3. nice :)

    I think you would be better turning word verification on, you receive lots of spam to your blog.

    ReplyDelete
  4. uggs clearance There are various designs and variations available in such Uggs Outlet types of boots and also you can be assured that you will occur throughout the shoes that you desire at an affordable price.Most of those boots are made of sheep skin and are of Australian brand but there is commonly a institution within of the united states which manufactures these shoes that are extremely fashionable ugg boots clearance and look funky on both grownup men and women.

    ReplyDelete
  5. That was a great message in my carrier, and It's wonderful commands like mind relaxes with understand words of knowledge by information's.
    Java Training in Chennai

    ReplyDelete
  6. Thanks for sharing these information. It’s a very nice topic. We are providing online training classes

    Best Linux training in Noida
    Linux Training Institute in Noida

    ReplyDelete
  7. Thank you for your post, I look for such article along time, today i find it finally. this post give me lots of advise it is very useful for me.

    Best bca college in noida
    Top bca colleges in noida

    ReplyDelete
  8. Thank you very much for providing important information. All your information is very valuable to me.
    Village Talkies a top-quality professional corporate video production company in Bangalore and also best explainer video company in Bangalore & animation video makers in Bangalore, Chennai, India & Maryland, Baltimore, USA provides Corporate & Brand films, Promotional, Marketing videos & Training videos, Product demo videos, Employee videos, Product video explainers, eLearning videos, 2d Animation, 3d Animation, Motion Graphics, Whiteboard Explainer videos Client Testimonial Videos, Video Presentation and more for all start-ups, industries, and corporate companies. From scripting to corporate video production services, explainer & 3d, 2d animation video production , our solutions are customized to your budget, timeline, and to meet the company goals and objectives.
    As a best video production company in Bangalore, we produce quality and creative videos to our clients.

    ReplyDelete
  9. Very informative blog thank you for sharing.

    To make your career development the best by learning software courses by getting trained from the best software training institute in Chennai. Just select your fav... course given below.
    php course in chennai
    RPA Training in Chennai
    Azure Training in Chennai
    DevOps Training in Chennai
    Cloud-computing Training in Chennai
    Blue-Prsim Training in Chennai
    Ui-Path Training in Chennai

    ReplyDelete


  10. Nice blog thank you .For your Sharing It's a pleasure to read your post.It's full of information I'm looking for and I'd like to express that "The content of your post is awesome"
    javascript training in chennai
    mysql training in chennai
    unix training in chennai

    ReplyDelete

  11. Nice blog thank you .For your Sharing It's a pleasure to read your post.It's full of information I'm looking for and I'd like to express that "The content of your post is awesome"

    Dotnet Training in Chennai
    Core java Training in Chennai
    Web design Training in Chennai

    ReplyDelete
  12. Whatsapp Number Call us Now! 01537587949
    outsourcing in Bangladesh
    USA pone web iphone repair USA
    USA SEX WEB careful
    bd sex video B tex
    bd sex video sex video
    bd sex video freelancing course

    ReplyDelete
  13. Great analysis on the null pointer bug in `setHelpMenu`! It’s surprising this issue has persisted for so long. Kudos for filing a bug report—hopefully, it leads to a fix. Keep up the good work!
    bca internship | internship for bca students | sql internship | online internship for btech students | internship for 1st year engineering students

    ReplyDelete
  14. Join our Java training in Coimbatore to learn from industry experts and gain hands-on coding experience. Start your journey towards a successful tech career today

    ReplyDelete
  15. Solid breakdown of DevOps culture. For anyone interested in a career, PlacementPS.com is truly a great place to master DevOps with excellent mentoring and placement guidance.

    Best Azure Devops training in chennai
    Best AWS Course in Chennai
    aws-devops-certification-training-in-chennai
    azure-course-in-chennai
    google-cloud-training-in-chennaii

    ReplyDelete
  16. Your blog provided valuable insights that we can really work with. Every tip shared in your post is incredibly useful. Code++, Software Training Institute in Coimbatore

    ReplyDelete
  17. "Enroll in our power bi course to master data visualization and analytics. Gain hands-on skills to transform raw data into insightful business reports."

    ReplyDelete
  18. Good overview of online backend learning.Python Django is widely used in projects.Practice-based courses work best.This Python Django online course covers real coding scenarios.It seems suitable for remote learners.

    ReplyDelete
  19. Learn data modeling techniques like ERDs, normalization, and dimensional modeling. Design efficient databases and optimize data structures using tools like SQL and ERwin.data modeling free online course

    ReplyDelete
  20. Master DevOps practices like CI/CD, automation, and cloud platforms (AWS, Azure). Learn to use tools such as Docker, Kubernetes, and Jenkins to streamline the software development lifecycle.devops classes

    ReplyDelete
  21. Good overview of online backend learning.Python Django is widely used in projects.Practice-based courses work best.devops training courses

    ReplyDelete
  22. Animation assignment help in Australia is a great support system for students studying creative and technical animation subjects. These assignments often require strong software skills, creativity, and technical accuracy. Professional help allows students to understand animation concepts, tools, and project structuring better. Expert guidance ensures assignments meet academic criteria while improving students’ practical skills and confidence in animation-related coursework.

    ReplyDelete