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

  • SEARCH
  • Home
  • 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 8836775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:34:17+00:00 2026-06-14T09:34:17+00:00

I want to set an Integer to a specific value, which is 0 or

  • 0

I want to set an Integer to a specific value, which is 0 or the attribute found in another class. Since instances of this class are stored in a Map of Lists, but this map may be null at the point, I wonder which of the both ways to handle this are better.

Integer value = 0;
if (myMap != null && 
    myMap.get(keyForList) != null && 
    myMap.get(keyForList).get(0) != null) {
    value = myMap.get(keyForList).get(0).getAttribute();
}

Or the way which I would consider better and more efficient:

Integer value = 0;
try {
    value = myMap.get(keyForList).get(0).getAttribute();
} catch (NullPointerException e) {
    // without doing anything value is 0 as expected 
}

Thanks for any help!

  • 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-14T09:34:18+00:00Added an answer on June 14, 2026 at 9:34 am

    The standard advice is that exceptions shouldn’t be used for flow control. They’re relatively heavy-weight, break the standard flow of control and consequently can be difficult to follow. They should be used for exceptional circumstances.

    That’s not to say that you can’t use them to recover from conditions, but if it’s easy/more explicit to check for (say) null or zero, then you should do that in preference.

    I would note (however) that Java is verbose and doesn’t have simple operations for handling defaults/nulls in code such as:

    Integer result = a.getB().getC().getD();
    

    As such, my advice is:

    1. check out the null object pattern, which means that you can get away with no null checks in the above scenario
    2. code like the above points to a real lack of OO functionality. The code above should get object a itself to get b, and object b would get c, and so on. In it’s current form it breaks the Law of Demeter and exposes to you how a, b, c etc are composed. Remember that OO is about getting objects to do things for you, not having them tell you what they’re composed of and having you do it yourself.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to calculate the smallest integer with exactly k bits set, that is
i want set the visibility to itemized overlay in map view. if the zoom
i want set class for repeater when doPostBack for those dataID i wan but
i have a multiple marker Google map, it works fine but i want set
I want an algorithm (no specific language) to find a subset from a set
This is a follow-up question to this one: Check for specific integer in a
I have a set of integers. I want to find the longest increasing subsequence
I want set interfaceOrientation in one UIView as LandscapeLeft and Portrait in other UIView.
I want set Listbox background to transparent but not working Is there any idea?
My question is : I have an EditText and I want set a margin

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.