Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPE with null values #28

Open
rvansa opened this issue Jul 19, 2021 · 3 comments
Open

NPE with null values #28

rvansa opened this issue Jul 19, 2021 · 3 comments

Comments

@rvansa
Copy link
Member

rvansa commented Jul 19, 2021

if(!this.get(key).equals(other.get(key))){

This line fails with NPE if the value is null.

@shivam-sharma7
Copy link
Member

@rvansa

if (thisValue == null && otherValue == null) {
            // Both values are null, consider them equal
            continue;
        }
        
        if (thisValue == null || otherValue == null || !thisValue.equals(otherValue)) {
            return false;
        }
    }
}

In this modified version, before invoking the equals() method, it first checks if either thisValue or otherValue is null. If both values are null, they are considered equal, and the loop proceeds to the next key. If either value is null or the values are not equal, the method returns false.

By including the null check, you can prevent the NullPointerException from occurring when comparing values that can be null.

@johnaohara
Copy link
Member

@shivam-sharma7 do you want to open a PR to fix this issue?

@shivam-sharma7
Copy link
Member

shivam-sharma7 commented Jun 9, 2023

@johnaohara I'm not sure about this. What you thing about the code?
#28 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants