无法读取数组长度,因为 “this.weeklyHours” 为空。

huangapple go评论66阅读模式
英文:

Cannot read the array length because "this.weeklyHours" is null

问题

我在calculateTotalPay方法和calculateAveragePay方法中遇到了java.lang.NullPointerException错误。错误消息指出它无法读取数组长度,因为this.weeklyHoursnull

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];

huangapple
  • 本文由 发表于 2023年6月16日 07:14:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486039.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定