Reputation: 150
This is my equal method:
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;
}
}
For some reason, this line gives me a null pointer exception: this.genre.equals(other.genre)
genre is a variable from another package;
All variables were declared in the class Car. The program compiles fine.
This is the constructor of Car:
public Car(double price, float height, String genre, String name, int door)
{
super(price, height, genre);
this.name = name;
this.door = door;
}
Why am I getting a null pointer exception?
Upvotes: 0
Views: 992
Reputation: 92274
Because this.genre
is null?
if (this.genre != null && this.genre.equals(other.genre))
Upvotes: 2
Reputation: 41200
I suspect you are equaling with null
String genre
in Car Object which cause NullPointerExcaption
.
if (this.genre!=null && this.genre.equals(other.genre)){...}
One more thing -
...
if(...this.door = other.door){
^^^^
It should be ==
.
Upvotes: 0
Reputation: 38300
Start using a library that correctly handles nulls when performing string comparisons.
Start using apache commons lang. Here is the StringUtils JavaDoc page
Upvotes: 0