[英]equals() gives me null pointer exception
這是我的平等方法:
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)
流派是另一個包的變量;
所有變量都在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))
在執行字符串比較時,開始使用正確處理空值的庫。
開始使用apache commons lang。 這是StringUtils JavaDoc頁面
我懷疑你在Car Object中等於null
String genre
導致NullPointerExcaption
。
if (this.genre!=null && this.genre.equals(other.genre)){...}
還有一件事 -
...
if(...this.door = other.door){
^^^^
它應該是==
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.