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

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

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

问题

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

  1. public double calculateAveragePay() {
  2. double averagePay = 0;
  3. double totalPay = 0;
  4. // totalPay = calculateTotalPay();
  5. averagePay = totalPay / weeklyHours.length;
  6. return averagePay;
  7. }
  8. public double calculateTotalPay() {
  9. int totalAmount = 0;
  10. // for (int index = 0; index < weeklyHours.length; index++)
  11. totalAmount += weeklyHours[index];
  12. return totalAmount;
  13. }
  1. public class Employee {
  2. private static final int DEFAULT_WEEKS = 52;
  3. private String name;
  4. private double salary;
  5. private double[] weeklyHours;
  6. //private int numWeeks;
  7. public Employee(String name, double salary) {
  8. this.name = name;
  9. this.salary = salary;
  10. double[] weeklyHours = new double[DEFAULT_WEEKS];
  11. }
  12. public Employee(String name, double salary, int numWeeks) {
  13. this.name = name;
  14. this.salary = salary;
  15. double[] weeklyHours = new double[numWeeks];
  16. }
  17. }
英文:

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.

  1. public double calculateAveragePay() {
  2. double averagePay = 0;
  3. double totalPay = 0;
  4. // totalPay = calculateTotalPay();
  5. averagePay = totalPay / weeklyHours.length;
  6. return averagePay;
  7. }
  8. public double calculateTotalPay() {
  9. int totalAmount = 0;
  10. // for (int index = 0; index < weeklyHours.length; index++)
  11. totalAmount += weeklyHours[index];
  12. return totalAmount;
  13. }
  1. public class Employee {
  2. private static final int DEFAULT_WEEKS = 52;
  3. private String name;
  4. private double salary;
  5. private double[] weeklyHours;
  6. //private int numWeeks;
  7. public Employee(String name, double salary) {
  8. this.name = name;
  9. this.salary = salary;
  10. double[] weeklyHours = new double[DEFAULT_WEEKS];
  11. }
  12. public Employee(String name, double salary, int numWeeks) {
  13. this.name = name;
  14. this.salary = salary;
  15. double[] weeklyHours = new double[numWeeks];
  16. }

答案1

得分: 2

你在两个构造函数中都有相同的错误。你正在用一个新的局部变量遮蔽了字段。像你对其他字段做的那样使用 this 来避免这类错误。

  1. public Employee(String name, double salary) {
  2. this.name = name;
  3. this.salary = salary;
  4. this.weeklyHours = new double[DEFAULT_WEEKS];
  5. }
  6. public Employee(String name, double salary, int numWeeks) {
  7. this.name = name;
  8. this.salary = salary;
  9. this.weeklyHours = new double[numWeeks];
  10. }

你可以用以下方式替换第一个构造函数:

  1. public Employee(String name, double salary) {
  2. this(name, salary, DEFAULT_WEEKS);
  3. }
英文:

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.

  1. public Employee(String name, double salary) {
  2. this.name = name;
  3. this.salary = salary;
  4. // double[] weeklyHours = new double[DEFAULT_WEEKS];
  5. this.weeklyHours = new double[DEFAULT_WEEKS];
  6. }
  7. public Employee(String name, double salary, int numWeeks) {
  8. this.name = name;
  9. this.salary = salary;
  10. // double[] weeklyHours = new double[numWeeks];
  11. this.weeklyHours = new double[numWeeks];
  12. }

You could replace the first constructor with

  1. public Employee(String name, double salary) {
  2. this(name, salary, DEFAULT_WEEKS);
  3. }

答案2

得分: 0

_Employee_类的构造方法内部,您正在初始化一个_局部变量_,而不是分配_类变量_。

  1. double[] weeklyHours = new double[DEFAULT_WEEKS];
  1. double[] weeklyHours = new double[numWeeks];

只需删除声明类型,即可访问_类变量_。

  1. weeklyHours = new double[DEFAULT_WEEKS];
  1. weeklyHours = new double[numWeeks];
英文:

From within the Employee class constructor methods, you're initializing a local variable, as opposed to assigning the class variable.

  1. double[] weeklyHours = new double[DEFAULT_WEEKS];
  1. double[] weeklyHours = new double[numWeeks];

Just remove the declaration type, to access the class variable.

  1. weeklyHours = new double[DEFAULT_WEEKS];
  1. 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:

确定