Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
@Override
	public boolean equals(Object obj) {
		
		if (this == obj) {
			return true;
		}
		if (obj == null || getClass() != obj.getClass()) {
			return false;
		}
		Customer p1 = (Customer) obj;
		return this.name.equals(p1.name) && this.customerId == (p1.customerId) && this.city.equals(p1.city)
				&& this.phone.equals(p1.phone) && this.email.equals(p1.email);
	}


What I have tried:

I added one more if condition in the above method:

if (p1.city == null || p1.name == null || p1.email == null || p1.phone == null) {
			return false;
		}

But then it shows assertion error.
Posted
Updated 24-Jan-22 6:02am
v3
Comments
Richard MacCutchan 23-Jan-22 3:17am    
Which line throws the error? And more importantly, which item is null?

You added a line that will check that the string elements of the p1 instance of the Customer class for null but you have not added a check for the string elements of the this instance class.
Java
@Override
public bool equals(object obj)
{
	if (obj == null ) {
        return false;
    }

	if (this == obj) {
		return true;
	}

	if (obj == null || getClass() != obj.getClass()) {
		return false;
	}

	Customer p1 = (Customer) obj;

	if (this.name == null || p1.name == null) {
		return false;
	}

	if (this.city == null || p1.city == null) {
		return false;
	}

	if (this.phone == null || p1.phone == null) {
		return false;
	}

	if (this.email == null || p1.email == null) {
		return false;
	}


	return this.name.equals(p1.name) && this.id.equals(p1.id) && this.city.equals(p1.city) && this.phone.equals(p1.phone) && this.email.equals(p1.email);
}
 
Share this answer
 
This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterday's shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, it will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, the debugger will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Comments
Sourabh Jambale 23-Jan-22 3:30am    
Actually, the compiler adds input of its own when I submit the solution(when I run the program, I don't get any errors because I initialised the objects to their correct values) it shows Null Pointer Exception at the equals() method. This is my main method:

public class Source {
	public static void main(String[] args) {
		Customer c1 = new Customer(1, "Sourabh", "Pune", "89", "Hello");
		Customer c2 = new Customer(1, "Sourabh", "Pune", "89", "hello");
		c1.setEmail("vinod@gmail.com");
		c2.setEmail("vinod@gmail.com");
		c2.equals(c1);
		Customer c3 = new Customer();
		c3.setCustomerId(10);
		int k = c3.getCustomerId();
		System.out.println(k);
		Customer c4 = new Customer();
		c3.equals(c4);
	}

}
OriginalGriff 23-Jan-22 3:52am    
Start by googling "Java debugger" and work from there ... it's not the same thing as a compiler at all.
Solution 2 is not correct, because it's perfectly fine if the property phone of both objects is null.

// copy of  Apache Commons' StringUtils.equals()
private static boolean myequals(String str1, String str2) {
    return str1 == null ? str2 == null : str1.equals(str2);
}

.....
	Customer p1 = (Customer) obj;
    return myequals(this.name, p1.name) && myequals(this.id, p1.id) && myequals(this.city, p1.city) && myequals(his.phone,p1.phone) && myequals(this.email, p1.email);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900