Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 880773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:09:12+00:00 2026-05-15T12:09:12+00:00

The following code throws NullPointerException : int num = Integer.getInteger(123); Is my compiler invoking

  • 0

The following code throws NullPointerException:

int num = Integer.getInteger("123");

Is my compiler invoking getInteger on null since it’s static? That doesn’t make any sense!

What’s happening?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:09:13+00:00Added an answer on May 15, 2026 at 12:09 pm

    The Big Picture

    There are two issues at play here:

    • Integer getInteger(String) doesn’t do what you think it does
      • It returns null in this case
    • the assignment from Integer to int causes auto-unboxing
      • Since the Integer is null, NullPointerException is thrown

    To parse (String) "123" to (int) 123, you can use e.g. int Integer.parseInt(String).

    References

    • Java Language Guide/Autoboxing

    Integer API references

    • static int parseInt(String)
    • static Integer getInteger(String)

    On Integer.getInteger

    Here’s what the documentation have to say about what this method does:

    public static Integer getInteger(String nm): Determines the integer value of the system property with the specified name. If there is no property with the specified name, if the specified name is empty or null, or if the property does not have the correct numeric format, then null is returned.

    In other words, this method has nothing to do with parsing a String to an int/Integer value, but rather, it has to do with System.getProperty method.

    Admittedly this can be quite a surprise. It’s unfortunate that the library has surprises like this, but it does teach you a valuable lesson: always look up the documentation to confirm what a method does.

    Coincindentally, a variation of this problem was featured in Return of the Puzzlers: Schlock and Awe (TS-5186), Josh Bloch and Neal Gafter’s 2009 JavaOne Technical Session presentation. Here’s the concluding slide:

    The Moral

    • Strange and terrible methods lurk in libraries
      • Some have innocuous sounding names
    • If your code misbehaves
      • Make sure you’re calling the right methods
      • Read the library documentation
    • For API designers
      • Don’t violate the principle of least astonishment
      • Don’t violate the abstraction hierarchy
      • Don’t use similar names for wildly different behaviors

    For completeness, there are also these methods that are analogous to Integer.getInteger:

    • Boolean.getBoolean(String)
    • Long.getLong(String)

    Related questions

    • Most Astonishing Violation of the Principle of Least Astonishment
    • Most awkward/misleading method in Java Base API ?

    On autounboxing

    The other issue, of course, is how the NullPointerException gets thrown. To focus on this issue, we can simplify the snippet as follows:

    Integer someInteger = null;
    int num = someInteger; // throws NullPointerException!!!
    

    Here’s a quote from Effective Java 2nd Edition, Item 49: Prefer primitive types to boxed primitives:

    In summary, use primitives in preference to boxed primitive whenever you have the choice. Primitive types are simpler and faster. If you must use boxed primitives, be careful! Autoboxing reduces the verbosity, but not the danger, of using boxed primitives. When your program compares two boxed primitives with the == operator, it does an identity comparison, which is almost certainly not what you want. When your program does mixed-type computations involving boxed and unboxed primitives, it does unboxing, and when your program does unboxing, it can throw NullPointerException. Finally, when your program boxes primitive values, it can result in costly and unnecessary object creations.

    There are places where you have no choice but to use boxed primitives, e.g. generics, but otherwise you should seriously consider if a decision to use boxed primitives is justified.

    Related questions

    • What is the difference between an int and an Integer in Java/C#?
    • Why does autoboxing in Java allow me to have 3 possible values for a boolean?
    • Is it guaranteed that new Integer(i) == i in Java? (YES!!!)
    • When comparing two Integers in Java does auto-unboxing occur? (NO!!!)
    • Java noob: generics over objects only? (yes, unfortunately)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 451k
  • Answers 451k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think you are confusing objects and classes. A class… May 15, 2026 at 9:01 pm
  • Editorial Team
    Editorial Team added an answer I don't fully understand your question. The way you've got… May 15, 2026 at 9:01 pm
  • Editorial Team
    Editorial Team added an answer Just increment a binary number and take the elements corresponding… May 15, 2026 at 9:01 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.