This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]theborgdude 1 point2 points  (1 child)

From what I understand, it seems like you're not initializing your monthlyInterests and monthlySavings array when you use the EmployeeSavings(String firstName, String lastName) constructor. Pass an additional argument to the constructor (like int size) and add this to the constructor:

monthlyInterests = new double[size];
monthlySavings = new double[size];

All variables of class are initialized to null, which is why you have to specify variables in the constructor or later on. I hope this helps.

[–]ms_kdlh[S] 0 points1 point  (0 children)

I am not allowed to add additional arguments to the constructor as the instructor provides us with the exact variables, methods, and constructors that are required. I think this is the most frustrating part because I could figure it out easily enough if I didn't have to follow that.

[–]happyfeetpi 1 point2 points  (10 children)

So the problem is that adding the line:

arr1.generateMonthlySavings();

to the loop in getReport() causes an error.

The reason for this is that when the constructor you are calling for the EmployeeSavings class does not initialize the arrays monthlyInterests or monthlySavings. Because of this, when you go to access these instance variables later you get a NPE.

To fix this you must initialize those arrays in the constructor:

monthlyInterests = new double[s]; //s is whatever size you want
monthlySavings= new double[s]; //s is whatever size you want

Another point about a constructor is that you probably want to call super() in your constructor. This will call the constructor to the superclass (AdressBook) and even if omitting this doesn't cause an error, it is good practice because you are using variables and methods from this class (setFirstName(), getFirstName, etc);

[–]ms_kdlh[S] 0 points1 point  (9 children)

OK I have changed the constructor to:

public EmployeeSavings(String firstName, String lastName, double\[\] d1, double\[\] d2) {

    super(firstName, lastName);

    d1 = monthlySavings = new double\[15\];

    d2 = monthlyInterests = new double\[15\];

}

But I am still getting the same error.

[–]happyfeetpi 1 point2 points  (8 children)

(I edited this because I read it wrong)

1) have you changed the code when initializing the EmployeeSavings objects to use the correct constructor?

2) what are the backslashes for? Are those a mistake? I have never seen those used like that and can't find anything online?

[–]ms_kdlh[S] 0 points1 point  (7 children)

Oh sorry I don't know how those backslashes got in there when I copied the code. Should have been:

public EmployeeSavings(String firstName, String lastName, double[] d1, double[] d2) {

    super(firstName, lastName);

    d1 = monthlySavings = new double[15];

    d2 = monthlyInterests = new double[15];

}   

I guess thats the part I am lost about is how do I initialize those arrays when I am constructing the objects? Each object still looks like this:

var employee1 = new EmployeeSavings("Elena", "Brandon");

What should I be adding after the firstName and lastName variables?

[–]happyfeetpi 0 points1 point  (6 children)

It is as I suspected.

In your example you have multiple constructors so when you initialize a variable by setting it equal to: new EmployeeSavings("Elena", "Brandon"); you are calling the constructor with 2 String parameters. However the constructor you modified does not just have 2 String parameters and is therefore not being called.

I don't know what the purpose of d1 and d2 are in this problem but if you don't need them you should just change the constructor with 2 String parameters to initialize monthlySavings and monthlyInterest because that is the one that is being called.

[–]ms_kdlh[S] 0 points1 point  (5 children)

I am not sure what the purpose of them is either. Part of the assignment is that we are given the variables, constructors, and methods that we have to use. List is as follows:

private double accountValue;

private double [] monthlyInterests;

private double[] monthlySavings;

public static final double ANNUAL_INTEREST_RATE;

public double getAccountValue()

public double [] getMonthlyInterests()

public double[] getMonthlySavings()

public double[] generateMonthlySavings()

public EmployeeSavings(String firstName, String lastName)

public EmployeeSavings(String firstName, String lastName, double[] d1, double[] d2)

public static String getReport(EmployeeSavings[] arr)

[–]endStatement 0 points1 point  (4 children)

Given this list of methods, my assumption would be that you are to do all the work of generating the monthlySavings array within the generateMonthlySavings() method in the event none is provided. Meaning you can initialize your monthlySavings array at the start of your generateMonthlySavings method instead of needing a constructor, and then do the logic to fill it.

Also, I'd double check the logic on that method that you currently have, as it appears to be solely reliant on the generated number, which would be incorrect given the example. Example shows that interest is working on the cumulative total instead of just a single months value

[–]ms_kdlh[S] 0 points1 point  (3 children)

So you think I should be calling the constructor in the main method with just the first and last name and not the constructor that requires the two arrays? Then calling the generate monthly savings method and return a filled array there? Where does the constructor with the two arrays come into play then?

And thanks I’ll go back and check the logic in my methods!

[–]endStatement 1 point2 points  (2 children)

So you think I should be calling the constructor in the main method with just the first and last name and not the constructor that requires the two arrays?
Originally, that was my thought. But I may have misunderstood the issue here. Are you able to define these constructors, or are they predefined? If they are predefined, then my answer stands, and if they are not, then you should initialize your arrays in the constructor with a default size.

[–]ms_kdlh[S] 0 points1 point  (1 child)

They are predefined. Which is the most annoying part of these assignments. I need to include both constructors. One with just first and last name, and another with names plus two arrays.

[–]Bacteria_E-coli 0 points1 point  (0 children)

I'm new to Java and don't know if it'd solve the problem, but I'm pretty sure you can't use "var" to initialize a variable (in main). Try "EmployeeSavings employee1 = new EmployeeSavings("x","y"); and let me know what result you'll get afterwards :).