英文:
Cannot read the array length because "this.weeklyHours" is null
问题
我在calculateTotalPay
方法和calculateAveragePay
方法中遇到了java.lang.NullPointerException
错误。错误消息指出它无法读取数组长度,因为this.weeklyHours
是null
。
public double calculateAveragePay() {
double averagePay = 0;
double totalPay = 0;
// totalPay = calculateTotalPay();
averagePay = totalPay / weeklyHours.length;
return averagePay;
}
public double calculateTotalPay() {
int totalAmount = 0;
// for (int index = 0; index < weeklyHours.length; index++)
totalAmount += weeklyHours[index];
return totalAmount;
}
public class Employee {
private static final int DEFAULT_WEEKS = 52;
private String name;
private double salary;
private double[] weeklyHours;
//private int numWeeks;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
double[] weeklyHours = new double[DEFAULT_WEEKS];
}
public Employee(String name, double salary, int numWeeks) {
this.name = name;
this.salary = salary;
double[] weeklyHours = new double[numWeeks];
}
}
英文:
I am getting a java.lang.NullPointerException
error for my calculateTotalPay
method and calculateAveragePay
method. The error message states that it cannot read the array length because this.weeklyHours
is null
.
public double calculateAveragePay() {
double averagePay = 0;
double totalPay = 0;
// totalPay = calculateTotalPay();
averagePay = totalPay / weeklyHours.length;
return averagePay;
}
public double calculateTotalPay() {
int totalAmount = 0;
// for (int index = 0; index < weeklyHours.length; index++)
totalAmount += weeklyHours[index];
return totalAmount;
}
public class Employee {
private static final int DEFAULT_WEEKS = 52;
private String name;
private double salary;
private double[] weeklyHours;
//private int numWeeks;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
double[] weeklyHours = new double[DEFAULT_WEEKS];
}
public Employee(String name, double salary, int numWeeks) {
this.name = name;
this.salary = salary;
double[] weeklyHours = new double[numWeeks];
}
答案1
得分: 2
你在两个构造函数中都有相同的错误。你正在用一个新的局部变量遮蔽了字段。像你对其他字段做的那样使用 this
来避免这类错误。
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
this.weeklyHours = new double[DEFAULT_WEEKS];
}
public Employee(String name, double salary, int numWeeks) {
this.name = name;
this.salary = salary;
this.weeklyHours = new double[numWeeks];
}
你可以用以下方式替换第一个构造函数:
public Employee(String name, double salary) {
this(name, salary, DEFAULT_WEEKS);
}
英文:
You have the same bug in both constructors. You are shadowing the field with a new local variable. Use this
like you did with the other fields to avoid this class of error.
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
// double[] weeklyHours = new double[DEFAULT_WEEKS];
this.weeklyHours = new double[DEFAULT_WEEKS];
}
public Employee(String name, double salary, int numWeeks) {
this.name = name;
this.salary = salary;
// double[] weeklyHours = new double[numWeeks];
this.weeklyHours = new double[numWeeks];
}
You could replace the first constructor with
public Employee(String name, double salary) {
this(name, salary, DEFAULT_WEEKS);
}
答案2
得分: 0
从_Employee_
类的构造方法内部,您正在初始化一个_局部变量_,而不是分配_类变量_。
double[] weeklyHours = new double[DEFAULT_WEEKS];
double[] weeklyHours = new double[numWeeks];
只需删除声明类型,即可访问_类变量_。
weeklyHours = new double[DEFAULT_WEEKS];
weeklyHours = new double[numWeeks];
英文:
From within the Employee class constructor methods, you're initializing a local variable, as opposed to assigning the class variable.
double[] weeklyHours = new double[DEFAULT_WEEKS];
double[] weeklyHours = new double[numWeeks];
Just remove the declaration type, to access the class variable.
weeklyHours = new double[DEFAULT_WEEKS];
weeklyHours = new double[numWeeks];
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论