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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:49:14+00:00 2026-06-10T10:49:14+00:00

Are these comparisons always safe from creating a NullPointer Exception ? if( myObject ==

  • 0

Are these comparisons always safe from creating a NullPointer Exception ?

if( myObject == null || myObject.someMethod() == someValue )
{
    if( myObject == null && myObject.getAlwaysTrue() )
       {

       }
}

Is there some directional precedence in java for condition evaluation, apart from short circuiting ?

UPDATE: I Know myObject.anything() will throw a NullPointer. Its just that I have come across such code by other programmers, and I want to know if there’s a safe way of squeezing multiple checks along with a null check in a single condition. I’m looking for a good rule to stick to.

  • 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-10T10:49:16+00:00Added an answer on June 10, 2026 at 10:49 am

    No, this line is not safe:

    if( myObject == null && myObject.getAlwaysTrue() )
    

    If you know that myObject is null then you shouldn’t try to dereference it. If however you wrote this:

    if( myObject != null && myObject.getAlwaysTrue() )
    

    Then it would be safe. This is because && (and || for that matter) has short-circuit evaluation. If you write a && b and the expression a evaluates to false, then the expression b is not evaluated so it will not throw an exception. The left operand is always evaluated first.

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

Sidebar

Related Questions

There is a white paper comparison between these 2 servers in the area of
Are there any good technical comparisons available between the various game development options available.
I've found myself evaluating both of these libs. Apart from what the GraphicsMagick comparison
I've got a program written in c# where there are a lot of comparisons
Is there any comparison to show why JSP/ASP.Net is better from security point of
I am refreshing on algorithm theory (from Cormen). There is an exercise in the
I'm creating some maps from raster files using the raster package in R. I'd
My string comparison doesnt work.I tried compare,equals ,= and all of these methods doesnt
Does Kdiff3 have APIs available? Or is there any comparison tool's API available that
Doing a mysqli query and COUNTing the results, is there a preferred comparison operator

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.