java - equals() 给我空指针异常

标签 java nullpointerexception package

这是我的平等方法:

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/

相关文章:

Java错误无法找到或加载主类包

java - 将 Base64 编码的 md5 转换为可读字符串

java - JProgressBar背景色不出现

java - 为什么我在使用 useDelimiter 时在我的数组中得到一个空元素

java - NullPointerException Logcat 错误 Android

clojure - 确保 var 在 Clojure 中绑定(bind)

java - Spring JPA : What is the cost of saveandflush vs save?

java - 在 JSP 中使用 request.getParameter 会导致 NullPointerException

R包在批处理模式下不可用

java - Java 中包相对标识符路径