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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:56:51+00:00 2026-06-05T11:56:51+00:00

I know it’s not possible to call the equals method on a null object

  • 0

I know it’s not possible to call the equals method on a null object like that:

//NOT WORKING
            String s1 = null;
            String s2 = null;
            if(s1.equals(s2))
            {
                System.out.println("NOT WORKING :'(");
            }

But in my case I want to compare two objects from two database and these two objects can have attributes null…

So what is the method to compare two attributes, knowing that we are not sure that the value is null or filled.

This method is good or not ?

//WORKING
            String s1 = "test";
            String s2 = "test";
            if(s1 == s2 || s1.equals(s2))
            {
                System.out.println("WORKING :)");
            }

            //WORKING
            String s1 = null;
            String s2 = null;
            if(s1 == s2 || s1.equals(s2))
            {
                System.out.println("WORKING :)");
            }

I’m not sure because in this case it’s not working … :

//NOT WORKING
            String s1 = null;
            String s2 = null;
            if(s1.equals(s2)|| s1 == s2  )
            {
                System.out.println("NOT WORKING :'''(");
            }
  • 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-05T11:56:52+00:00Added an answer on June 5, 2026 at 11:56 am

    I generally use a static utility function that I wrote called equalsWithNulls to solve this issue:

    class MyUtils {
      public static final boolean equalsWithNulls(Object a, Object b) {
        if (a==b) return true;
        if ((a==null)||(b==null)) return false;
        return a.equals(b);
      }
    }
    

    Usage:

    if (MyUtils.equalsWithNulls(s1,s2)) {
      // do stuff
    }
    

    Advantages of this approach:

    • Wraps up the complexity of the full equality test in a single function call. I think this is much better than embedding a bunch of complex boolean tests in your code each time you do this. It’s much less likely to lead to errors as a result.
    • Makes your code more descriptive and hence easier to read.
    • By explicitly mentioning the nulls in the method name, you convey to the reader that they should remember that one or both of the arguments might be null.
    • Does the (a==b) test first (an optimisation which avoids the need to call a.equals(b) in the fairly common case that a and b are non-null but refer to exactly the same object)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I KNOW I did this in WP7 (not WP7.1) and I can't figure out
I know that I can do something like $int = (int)99; //(int) has a
I know that < is not used inside an HTML tag. I know that
I know that it is possible to define recursive modules, does anyone know how
Know this might be rather basic, but I been trying to figure out how
Know if it's possible to access the iPhone compass in Safari using JavaScript? I
I know what I asking might not make a lot of sense for C#
I know that it's a subject that can raise a lot of debate, but
Anyone know if it is possible to make the PeriodicalUpdater pass the data it
I know that one of the differences between classes and structs is that struct

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.