• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

new to Java, how to instantiate a class

SWScorch

Diamond Member
I'm writing a quick little program right now, and unfortunately it has to be written in java, which I have never used before. I have a class called University, and a driver program which is supposed to instantiate an instance of the University class. However, I want to pass an array of objects to the University class. Here is what I'm trying:

public class University implements java.io.Serializable
{
Employee[] employees = new Employee[8];

public void University(Employee[] args)
{
employees = args;
}
}

public class ProgExc4
{
public static void main(String[] args)
{

Employee[] employees = new Employee[8]; // array of 8 Employee objects that bsu needs to know about
University bsu = new University(employees); // error here
University updatedBSU = new University(employees);

....

}

I get a compiler error: "cannot resolve symbol - constructor University (Employee[])"

Is it even possible to pass an array of objects to a new instance of a class through the constructor? I really wish I knew what I was doing.
 
Okay, I decided to go about it a different way, and just instantiate a new instance of the University class, and hard-code the Employee array within its constructor. Now I get a NullPointerException in the process method. I'm guessing the for loop isn't recognizing the Employee array. Any idea why? Here's my code:

http://home.twcny.rr.com/xcgeorge/pics/java/

ProgExc4 and University code is what's important.
 
Why are you passing an empty array of Employees to the constructor? Your University class already has an 8-member Employees array. There's no need to pass in a new one unless you initialize the Employees array in your ProgExc4 class prior to passing it to the constructor.

Are your ProgExc4 and University classes in the same directory?
 
biWeeklyNetPay is never initialized in your Employee subclasses.

NullPointerException occurs when printCheck() is called.
 
public void University(Employee[] args)
{
employees = args;
}

What you have there is a method.

public University(Employee[] args)
{
employees = args;
}

This is a constructor. There is no return type, and it is not a void. It's simply a constructor.

See attached for what it should look like.
 
Originally posted by: MrChad
Why are you passing an empty array of Employees to the constructor? Your University class already has an 8-member Employees array. There's no need to pass in a new one unless you initialize the Employees array in your ProgExc4 class prior to passing it to the constructor.

Are your ProgExc4 and University classes in the same directory?

That's how it looks now that I changed the code. Before, I made the array in ProgExc4 and tried passing it to University. That didn't work, so I just made the array in the University class.

Are you sure about the calcBiWeeklyPay not being initialized? I see that printCheck isn't there, bah. Thanks

BigJ: Thanks for clearing that up. I guess that subtlety escapd my attention.
 
Originally posted by: SWScorch
Originally posted by: MrChad
Why are you passing an empty array of Employees to the constructor? Your University class already has an 8-member Employees array. There's no need to pass in a new one unless you initialize the Employees array in your ProgExc4 class prior to passing it to the constructor.

Are your ProgExc4 and University classes in the same directory?

That's how it looks now that I changed the code. Before, I made the array in ProgExc4 and tried passing it to University. That didn't work, so I just made the array in the University class.

Are you sure about the calcBiWeeklyPay not being initialized? I see that printCheck isn't there, bah. Thanks

BigJ: Thanks for clearing that up. I guess that subtlety escapd my attention.

The double biWeeklyNetPay is not initialized. You call calcBiWeeklyPay, but that returns a double to the method caller. It does not initialize the biWeeklyNetPay variable in the Employee subclass (perhaps that's what you were intending?).
 
oh, I see what you mean. Yeah, so the way I'm doing it now I don't even need the variable biWeeklyNetPay because calcBiWeeklyNetPay is just returning that value to the calling statement.
 
Back
Top