Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 22544

I'm tring to compare two Objects, and I keep getting the same error (while comparing the name)

$
0
0

I'm trying to compare two Objects, by comparing their instance variables one by one. I keep getting the same error (while comparing the name only)

this is the error I keep getting: java.lang.AssertionError: The two instances are not equal, they have different names

Here is my code:

@Override    public boolean equals(Object obj) {        if (this == obj) return true;        if (obj == null || getClass() != obj.getClass()) return false;        Beverage beverage = (Beverage) obj;        if(this.name == null || beverage.name == null ) return false;        if (!Objects.equals(name, beverage.name)) return false;        if (Double.compare(beverage.price, price) != 0) return false;        if (Double.compare(beverage.discount, discount) != 0) return false;        return sugarLevel == beverage.sugarLevel;    }

The error is only with the name and the same message every time, I have tried multiple solutions but none of them worked


Viewing all articles
Browse latest Browse all 22544

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>