I have several classes, so I am going to display the important ones, so it clarify's them. I am just trapped with the java.lang.NullPointerException error under my Student class. Where would I enter the default value so my java.lang.NullPointerException gets fixed?
EDIT: There was no NullPointer involved. It was not setting up the constructor the correct way. I have found a solution.
public class UWECStudent extends UWECAcademic
{
private double gpa;
public UWECStudent(int uwecId, String firstName, String lastName,
double gpa)
{
super(uwecId, firstName, lastName);
this.gpa = gpa;
}
public void setNumTotalCredits(int numTotalCredits)
{
if(numTotalCredits <= 24)
{
System.out.println("Freshman");
}
if(numTotalCredits > 24 && numTotalCredits < 57)
{
System.out.println("Sophmore");
}
if(numTotalCredits > 58 && numTotalCredits < 85)
{
System.out.println("Junior");
}
if(numTotalCredits == 85)
{
System.out.println("Senior");
}
}
public final double getGpa()
{
return gpa;
}
public String toString()
{
return "UWECStudent = uwecId: " + getUwecId() + ", name: " +
getFirstName() + " " + getLastName() + ", title: " + getTitle() + ",
credits: " + getNumTotalCredits() + ", gpa: " + getGpa();
}
public boolean equals(Object other)
{
if(other instanceof UWECStudent)
{
UWECStudent o = (UWECStudent) other;
return getNumTotalCredits() == o.getNumTotalCredits() && gpa == o.gpa && super.equals(other);
}
else
{
return false;
}
}
}
The issue occurs in my equals method which is on the bottom. This class inherits from another one.
public abstract class UWECAcademic extends UWECPerson
{
private int numTotalCredits;
public UWECAcademic(int uwecId, String firstName, String lastName)
{
super(uwecId, firstName, lastName);
setNumTotalCredits(numTotalCredits);
}
public final int getNumTotalCredits()
{
return numTotalCredits;
}
public void setNumTotalCredits(int numTotalCredits)
{
this.numTotalCredits = numTotalCredits;
}
public abstract String toString();
public boolean equals(Object other)
{
if(other instanceof UWECAcademic)
{
UWECAcademic o = (UWECAcademic) other;
return numTotalCredits == o.numTotalCredits &&
super.equals(other);
}
else
{
return false;
}
}
}
Which this class inherits from another one. I will post it.
public abstract class UWECPerson
{
private int uwecId;
private String firstName;
private String lastName;
private String title;
public UWECPerson(int uwecId, String firstName, String lastName)
{
this.uwecId = uwecId;
this.firstName = firstName;
this.lastName = lastName;
}
public final int getUwecId()
{
return uwecId;
}
public final String getFirstName()
{
return firstName;
}
public final String getLastName()
{
return lastName;
}
public final String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public abstract String toString();
public boolean equals(Object other)
{
if(other instanceof UWECPerson)
{
UWECPerson o = (UWECPerson) other;
return uwecId == o.uwecId && firstName.equals(o.firstName) && lastName.equals(o.lastName) && title.equals(o.title);
}
else
{
return false;
}
}
}
I know this is very long, but I'm just confused on what to type in.
EDIT: Because this was marked as a duplicate, I am not allowed to post the correct answer here. I'll address the correct solution to this problem right here. It was mainly a failure of setting up the constructor. Here is how I solved my problem.
public class UWECStudent extends UWECAcademic
{
private double gpa;
public UWECStudent(int uwecId, String firstName, String lastName, double gpa)
{
super(uwecId, firstName, lastName);
this.gpa = gpa;
setNumTotalCredits(getNumTotalCredits());
setTitle(getTitle());
}
public void setNumTotalCredits(int numTotalCredits)
{
super.setNumTotalCredits(numTotalCredits);
if(numTotalCredits < 24)
{
setTitle("Freshman");
System.out.println("Freshman");
}
else if(numTotalCredits >=24 && numTotalCredits < 58)
{
setTitle("Sophomore");
System.out.println("Sophomore");
}
else if(numTotalCredits >= 58 && numTotalCredits < 86)
{
setTitle("Junior");
System.out.println("Junior");
}
else if(numTotalCredits >= 86)
{
setTitle("Senior");
System.out.println("Senior");
}
}
}
It was only that method and the constructor. That was about it.