2
votes

I'm getting an error in my code (it's the last line of my assignment and I can't seem to solve it) which is in the addStudent method. It says my attempt to access an overloaded constructor is actually attempting to access the default constructor, and that there are no variables for me to pass through. So, in a nutshell:

"studentArray[i] = new Student(firstname, lastname, gender, sClass, sID);"

is trying to access:

"public void Student(){}"

but I want it to access:

"public void Student(String firstname, String lastname, String gender, String sClass, String sID)"

Thanks for the help!

-AndresL

public void Student() 
{
    setFirstName("Unknown");
    setLastName("Unknown");
    setGender("Unknown");
    setClass("Freshman");
    setID("0000");
}

public void Student(String firstname, String lastname, String gender, String sClass, String sID) 
{
    setFirstName(firstname);
    setLastName(lastname);
    setGender(gender);
    setClass(sClass);
    setID(sID);
}

public static boolean addStudent(String firstname, String lastname, String gender, String sClass, String sID)
{
    for (int i=0; i<studentArray.length; i++) 
    {
        if (studentArray[i] == null)
        {
            studentArray[i] = new Student(firstname, lastname, gender, sClass, sID);
            totStudent++;
        }else {JOptionPane.showMessageDialog(null, "Max students reached.");}
    }
    return true;
}
5
After 1.5 years, I've realized.... that I'm an idiot. :pAndresL

5 Answers

4
votes

You are declaring your "constructors" with void, actually making them void methods, not constructors. Therefore, the compiler can only use the default constructor, which is why you receive that error. So: you should remove the void keyword anywhere you try to define a constructor.

2
votes

Remove void keywords from your constructors:

public void Student(String firstname, String lastname, String gender, String sClass, String sID) 

change to

public Student(String firstname, String lastname, String gender, String sClass, String sID) 

similarly

public void Student() 

change to

public Student() 

Constructors don't have return types.

1
votes

Constructors don't have a return type because they return a new Object of the same type as the class they are defined in, when you use them with the new keyword.

Object o = new Object(); // you are calling Object constructor.

-- EDIT -- Also I would recommend changing this

public Student() 
{
    setFirstName("Unknown");
    setLastName("Unknown");
    setGender("Unknown");
    setClass("Freshman");
    setID("0000");
}

to

public Student() 
{
    this("Unknown", "Unknown", "Unknown", "Freshman", "0000"); 
}
0
votes

constructors dont have a return type not even void. you are declaring your constructors with void, which would make them methods rather than constructors

0
votes

Remove the void from constructors and no need of calling setter methods. You may directly access the attributes as below:

 public void Student() 
 {
    this.firstName = "Unknown";
    this.lastName = "Unknown";
    this.gender = "Unknown";
    this.class = "Freshman";
    this.ID="0000";
 }

Similarly

 public void Student(String firstname, String lastname, 
                                         String gender, String sClass, String sID) 
 {
    this.firstName = firstname;
    this.lastName = lastname;
    this.gender = gender;
    this.class = sClass;
    this.ID=sID;
 }

You should be all set.