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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:23:22+00:00 2026-06-08T20:23:22+00:00

In the common situation where classes are embedded as attributes of other classes, what

  • 0

In the common situation where classes are embedded as attributes of other classes, what is the best way to check for nulls?

To illustrate what I’m talking about, say I want to access this.getObject1().getObject2().someMethod() where getObject1() or getObject2() could return null. Right now I am doing the following, and it’s ugly, and there has to be a better way:

if (this.getObject1() != null)
        if (this.getObject1().getObject2() != null)
            if (this.getObject1().getObject2().someMethod())
                return whatever;

To avoid a NPE, I have to check for nulls at each step before accessing someMethod(). What is a better way to accomplish this?

  • 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-08T20:23:23+00:00Added an answer on June 8, 2026 at 8:23 pm

    This style is to be avoided as far as possible – see the Law of Demeter – though I appreciate that library or legacy code may not leave you much option.

    Functional programming languages (in particular) avoid this problem through the use of Option or Maybe objects, which can be used in Java to some extent. See this article, for example. Long sequences of null checks can be avoided concisely using Scala for-comprehensions, for eaxmple. But back to Java…

    Ideally, assuming you can modify the code you are calling, you would restructure so you can call the ‘outer’ object, which will then call the ‘inner’ object, with only one null check at each stage. Or restructure the code to avoid such deep nesting.

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

Sidebar

Related Questions

I want to define some common post-construction behavior for a group of classes. When
I have a couple of classes that want to pass each other some information
I recently learned about Recursive Common Table Expressions (CTEs) while looking for a way
Common situation: I have a client on my server who may update some of
Common scenario: I have a library that uses other libraries. For example, a math
I have a set of classes which are all derived from a common base
I have a situation where I have 4-5 very similar classes which I'd like
What I'm looking right now is a set of classes derived from a common
If I have two classes that each extend a common parent, is it possible
I have a common library where I've put classes that are used between multiple

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.