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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:05:49+00:00 2026-05-29T23:05:49+00:00

Are there any patterns which are similar to Groovy’s safe navigation operator ? This

  • 0

Are there any patterns which are similar to Groovy’s safe navigation operator? This is where a null value does make sense and allowing a NullPointerException to be thrown would be wrong.

Other than checking for null, what are some other options? The String class provides a static method to dereference possibly null values but there doesn’t seem to be much use of this pattern apart from the Objects class. Google’s Guava project does have an Optional class but that requires the caller supplying a default which seems too much compared to the safe navigation operator and when you would just want it to be null most of the time. Something that looks like this:

com.google.common.base.Optional.of(fooBar).
    or(org.mockito.Mockito.mock(FooBar.class));

The null object pattern or the option monad in Scala are not what I’m looking for or proposals of adding such operators into future versions of Java, I’m looking for patterns that can be applied in Java now.

  • 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-29T23:05:50+00:00Added an answer on May 29, 2026 at 11:05 pm

    The “safe” navigation operator is in my opinion a really bad idea since it supresses exceptions and allows developers to get away with error-ridden code.

    There are basically just three cases to consider:

    • If you have a null where it is not supposed to be then you have an error and should fail fast and loud. A NullPointerException is a perfectly good way to do this, although some would argue that you should have tests which detected the stray null earlier. Note that Fail-Fast is a very important principle in building robust systems, so you should learn to love your NullPointerExceptions. They are there to guide you to building better software. You should fix the NPE by fixing your code, not by ignoring / suppressing it!
    • If a null is allowed but requires specific handling (e.g. a default value) then you should be explicit about this in your code. It’s perfectly good practice to write an if/else null check for this, or else you can use a helper function that itself handles the null case.
    • If a null is allowed but requires no specific handling then you don’t need to do anything. Just pass it round as usual.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are there any mnemonics or patterns that make memorizing emacs key combos easier?
Are there any patterns/solutions to solve the problem of having an API call which
Are there any patterns or best practices that can be used to simplify changing
Are there any templates/patterns/guides I can follow for designing a multithreaded server? I can't
Are there any commonly used patterns in Javascript for storing the URL's of endpoints
Are there any known design principles, best-practices and design patterns that one can follow
Are there any good resources (books, authoritative guides, etc.) for design patterns or other
I thought I would ask about this here. Does anyone know of any work
If yes, then are there any patterns that are specific to C++, and some
Is there any pattern how to deal with a lot of object instantiations (40k

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.