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 9112007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:38:01+00:00 2026-06-17T03:38:01+00:00

I guess I might be missing something obvious here, but anyway lets see the

  • 0

I guess I might be missing something obvious here, but anyway lets see the code.

public static class FreeMap extends TreeMap<String, Integer> {
    @Override
    public Integer put(String key, Integer value) {
        out.println(super.toString());
        out.println(super.getClass().getName()+" "+key+" : "+value);
        int i = super.put(key, value);//line 227
        assert this.size() == 1;
        return i;
    }

}
public static void main(String[] args) {
    FreeMap fm = new FreeMap();
    fm.put("A", 10);
}

On Running this you will get a output as following:

{}
com.xxxxxxxxxxxxxxxxxxxxxxxx.Graph$FreeMap A : 10
Exception in thread "main" java.lang.NullPointerException
at com.xxxxxxxxxxxxxxxxxxxxxxxx.Graph$FreeMap.put(Graph.java:227)
at com.xxxxxxxxxxxxxxxxxxxxxxxx.Graph.main(Graph.java:212)

I can see super is referring to FreeMap, not TreeMap, if it would have thrown a StackOverflow Exception I could have understood. Why nullpointerexception?

Thanks in advance

  • 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-06-17T03:38:02+00:00Added an answer on June 17, 2026 at 3:38 am

    Yes, because put returns the previous value:

    Returns:
    the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key.)

    There is no previous value, so it’s returning null, which you’re then unboxing, leading to an exception.

    Given that your method is already declared to return Integer, the fix is easy. Change this:

    int i = super.put(key, value);
    

    to this:

    Integer i = super.put(key, value);
    

    Or ideally, for readability:

    Integer oldValue = super.put(key, value);
    

    Note that this would also be more efficient in the case that there was already a value for the key – there’s no benefit from unboxing and reboxing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I might be missing something obvious but is there a reference somewhere about what
I might be missing something about the intended behavior of list extend, but why
Maybe I am missing something simple here, but it is frustrating me. So what
I guess there might be some overlapping with previous SO questions, but I could
I guess this might have been posted somewhere, I did search, but couldn't find
Aloha, Here's a simple class that overrides GetHashCode: class OverridesGetHashCode { public string Text
Self-teaching myself C++, and I know I'm missing something critical, but I can't for
Yes, as you might guess, I want to ask about Cloud-computing. I doubt that
My guess is that <br> might be inline and that <hr> might be block.
I guess getView() is called for each item of the list, but my question

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.