简体   繁体   English

Java:线程“main”java.lang.NullPointerException中的异常

[英]Java: Exception in thread “main” java.lang.NullPointerException

I keep getting this error when I run this code and I do not know how to fix it. 我运行此代码时不断收到此错误,但我不知道如何修复它。 I've been doing Java for about 3 months now and could use some help. 我已经做了大约3个月的Java并且可以使用一些帮助。 Basically the program asks the user to enter in the pay rate and the hours for each employee. 基本上该程序要求用户输入每位员工的工资率和工时。 Pay rate, hours and employee number are all arrays that are going to share the same index with each other. 支付率,小时数和员工数是所有将彼此共享相同索引的阵列。 When the user enters in the pay rate and hours for the employee, the program returns the gross pay for that employee. 当用户输入员工的工资率和工时时,程序将返回该员工的工资总额。 Except this error keeps popping up. 除了这个错误不断弹出。 Please help! 请帮忙!

Exception in thread "main" java.lang.NullPointerException
    at chapter.pkg7.lab.Payroll.setPayRate(Payroll.java:41)
    at chapter.pkg7.lab.Chapter7Lab.main(Chapter7Lab.java:45)
Java Result: 1

Here's the code: 这是代码:

package chapter.pkg7.lab;

import java.util.Scanner;

public class Chapter7Lab {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
 Payroll newEmployee = new Payroll();
       int[] employeeNum = newEmployee.getEmployeeID();
       double pay;
       int hour;

       for (int i = 0; i < employeeNum.length; i++)
       {
           System.out.println("Please enter the Pay Rate for Employee #" + employeeNum[i]);
           pay = keyboard.nextDouble();
           newEmployee.setPayRate(pay, i);

           System.out.println("Please enter the hours for Employee #" + employeeNum[i]);
           hour = keyboard.nextInt();
           newEmployee.setHours(hour, i);

           System.out.println("Gross pay is: " + newEmployee.getWages(i));
       }



package chapter.pkg7.lab;

public class Payroll {
    private int[] employeeID = new int[] {5658845, 4520125, 7895122, 
        8777541, 8451277, 1302850, 7580489};

    private int[] hours;
    private double[] payRate;
    private double wages;


   public double getWages(int index) {
      wages = hours[index] * payRate[index];
      return wages;
    }


   //Accessors and Mutators
    public int[] getEmployeeID() {
        return employeeID;
    }

    public void setEmployeeID(int[] employeeID) {
        this.employeeID = employeeID;
    }

    public int[] getHours() {
        return hours;
    }

    public void setHours(int hours, int index) {
        this.hours[index] = hours;
    }

    public double[] getPayRate() {
        return payRate;
    }

    public void setPayRate(double payRate, int index) {
        this.payRate[index] = payRate;
    }



}

You've declared payRate (and hours and wages ) as arrays, but it's an uninitialized instance variable, so it's null . 您已将payRate (以及hourswages )声明为数组,但它是未初始化的实例变量,因此它为null

Initialize it: 初始化:

private double[] payRate = new double[employeeID.length];

(and likewise for hours and wages ). (同样是hourswages )。

NullPointerException 

在您不初始化变量或对象并尝试访问该varibale或对象时出现

Add a constructor to your Payroll class that initialises the two arrays hours and payRate like this: 向您的Payroll类添加一个构造函数,初始化两个数组hourspayRate如下所示:

public Payroll(){
   hours = new int[employeeID.length];
   payRate = new double[employeeID.length];
}

Then when you create the Payroll object in your main class as you're currently doing with the line: Payroll newEmployee = new Payroll(); 然后,当您在主要类中创建Payroll对象时,您正在使用该行: Payroll newEmployee = new Payroll(); the arrays will be initialised. 数组将被初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM