0

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.

Adan Vivero
  • 422
  • 12
  • 36
  • *The issue occurs in my equals method* - Yes, what is the issue? – Scary Wombat Feb 28 '19 at 00:34
  • The question in the title is meaningless, since the `setNumTotalCredits(int numTotalCredits)` method in the `UWECStudent` class **doesn't "set" anything**, so whether it has been called or not makes no difference whatsoever. --- Besides that, since the `UWECAcademic` constructor calls the `setNumTotalCredits` method, it will **always have been called**. – Andreas Feb 28 '19 at 00:35
  • But for you general question: The default value of a field is `0`, `false`, or `null`, depending on field type (numeric, boolean, or reference). – Andreas Feb 28 '19 at 00:37
  • @Scary Wombat The issue is that I receive a java.lang.NullPointerException error under my Student class. Which is When a Student is created and his/her setNumTotalCredits method isn't called, I don't know how to implement a default number of credits in order for the equal method to return true under there. – Adan Vivero Feb 28 '19 at 00:39
  • Where would I place that? @Andreas – Adan Vivero Feb 28 '19 at 00:40
  • @ScaryWombat It's just java. I apologize – Adan Vivero Feb 28 '19 at 00:41
  • @AdanVivero Where would you place what? I've pointed out two fallacies in your question, and explained what the default values are. I haven't specified anything in my comments that could be "placed" anywhere, so I don't understand your comment. – Andreas Feb 28 '19 at 00:43
  • *Which is When a Student is created* I think that maybe you are confusing **declaration** with **instantiation** – Scary Wombat Feb 28 '19 at 00:46
  • @ScaryWombat could you show me a possible solution to this? – Adan Vivero Feb 28 '19 at 00:49
  • `UWECStudent student` == declaration `UWECStudent student = new UWECStudent ();` == instantiation – Scary Wombat Feb 28 '19 at 00:51
  • @Andreas unmark it as a duplicate. It didn't help. I figured out how to solve it. – Adan Vivero Feb 28 '19 at 20:46
  • @Andreas just to clarify, this whole problem had nothing to do with a NullPointer. I would like it if you could unmark it as that. – Adan Vivero May 06 '19 at 01:27
  • 1
    @AdanVivero - I don't think there is any point in unmarking this as duplicate. 1) You have (apparently) solved the problem. 2) This Q and your A are highly unlikely to be helpful to anyone but yourself. 3) The Q should also be closed as Off-topic: *"Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers."*; see https://stackoverflow.com/help/on-topic – Stephen C May 06 '19 at 02:01

0 Answers0