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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:03:46+00:00 2026-06-11T09:03:46+00:00

I have a Java command that looks something like below: Foo f = new

  • 0

I have a Java command that looks something like below:

Foo f = new Foo();
String string = f.format(new Bar().getSelection());
                       // ^ may be null

Sometimes it’s possible that my Bar object returns null, this is by design.

To me, the natural thing to do is to split the commands into multiple lines and do a null-check separately, such as:

Foo f = new Foo();
BarSel bs = new Bar().getSelection();
String string = "";
if (bs != null) {
    string = f.format(bs);
    // continue...
}

However, I’m wondering if there is a way to do this in one line? Is it possible to null-check objects inline?

I seem to remember reading about being able to use question mark, but I can’t recall the exact syntax and I might be wrong about that. Note that I’m not referring to the ternary operator, although that is another valid approach.

  • 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-11T09:03:47+00:00Added an answer on June 11, 2026 at 9:03 am

    I think you remember the elvis operator ( http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/000047.html ) which was rejected ( https://blogs.oracle.com/darcy/entry/project_coin_final_five ) from project coin.

    However, the jakarta commons have nvl-like functions such as http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/StringUtils.html#defaultString(java.lang.String) Even though these functions are not standard java, they end up in most programs as dependecy of some library.

    The problem you have is a common when you design a function with a valid null result: You have to document and handle the special case everywhere. I can pretty much guarantee you that someone will mess this up in maintenance, resulting in spurious null pointer exceptions. That’s why i would, in general, recommend designs that don’t return null.

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

Sidebar

Related Questions

I have a section in a POM that looks something like this: <scm> <connection>scm:cvs:ext:myhostname:/cvsroot/repo:module_name</connection>
I have a folder organization that looks like this: link.sh dist/MyApp-3.0.0/script.sh dist/MyApp-3.0.0/lib/*.jar The link.sh
I have an XML that looks like this and a corresponding xsd with it.
I have next command in my batch script dir project\*.java /s /B > temp_file
Noob question, apologies. I'm compiling Java in Windows Vista's command-line and have so many
I have Java Map (out of Strings and Ints) objects that I want to
I have a java program that generates an HTML file. The Java program takes
I have a Java application that consists of a client and a server. The
I am working on a project that will have several Java classes that are
I'm working on creating a command line game in Java. The basic premise that

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.