这是我的平等方法:
public boolean equals(Car other)
{
if (other == null)
{
return false;
}
if (this.genre.equals(other.genre))
{
if (this.price == other.price && this.height == other.height && this.name == other.name && this.door = other.door)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
由于某种原因,这一行给了我一个空指针异常:this.genre.equals(other.genre)
genre 是另一个包中的变量;
所有变量均在 Car 类中声明。该程序编译良好。
这是 Car 的构造函数:
public Car(double price, float height, String genre, String name, int door)
{
super(price, height, genre);
this.name = name;
this.door = door;
}
为什么我会收到空指针异常?
最佳答案
因为this.genre
为空?
if (this.genre != null && this.genre.equals(other.genre))
关于java - equals() 给我空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14693306/