调用类方法时出错java.lang.NullPointerException

时间:2015-10-02 21:33:04

标签: java class

我试图制作一个程序,其中包括姓名,开始日期,员工薪水。所以我做了几个课程,包括我的通话课程:

TestWorker.java

public class TestWorker {
    public static void main(String[] args) {
        Worker w1, w2, w3;
        w1 = new Worker ("Robert  William Hunter", "23/10/2005", 35000.00);
        w2 = new Worker ("John Smith", "15/11/2005", 25000.00);
        w3 = new Worker ("Mary Jane Hull", "06/09/2007");
        w2. setSalary(20000.00);
        w2.setSupervisor(w1);
        w3.setSupervisor(w1);
        System.out.println("Number of workers = " + Worker.getHowManyWorkers() +" \n");
        System.out.println("Supervisor of John is " + w2.getSupervisorName());
        System.out.println(w1.toString()+" \n");
        System.out.println(w2.toString()+" \n");
        System.out.println(w3.toString()+" \n");
    }
}

worker.java

public class Worker {
    private Name workerName;
    private MyDate dateJoiningCompany;
    private Worker Supervisor;
    public Worker(String name, String date, double salary) {
        workerName = new Name(name);
        dateJoiningCompany = new MyDate(date);
        Salary = (float)salary;
    }
    public Worker(String name, String date) {
        workerName = new Name(name);
        dateJoiningCompany = new MyDate(date);
    }

    public void setSupervisor(Worker supervisor) {
        if(supervisor != null) {
            Supervisor.workerName = supervisor.workerName;  //ERROR HERE
            Supervisor.dateJoiningCompany = supervisor.dateJoiningCompany;
        }
        else {
            System.out.println("The person you are trying to assign a Supervisor to has no supervisor");
        }
    }
    public String toString() {
        if(Supervisor != null) {
            return (workerNumber + " " + workerName.toString() + " " + dateJoiningCompany.toString() + " " + Supervisor.workerName.toString() + " " + Salary);
        }
        else {
            return (workerNumber + " " + workerName.toString() + " " + dateJoiningCompany.toString() + " " + Salary);
        }
    }
}

Name.java

import java.util.StringTokenizer;

public class Name {
    private String firstName;
    private String middleName;
    private String lastName;
    public Name(String name) {
        StringTokenizer tokens;
        tokens = new StringTokenizer(name," ");
        int numTokens = tokens.countTokens();
        if(numTokens == 2) {
            firstName = tokens.nextToken();
            middleName = null;
            lastName = tokens.nextToken();
        }
        else if(numTokens == 3) {
            firstName = tokens.nextToken();
            middleName = tokens.nextToken();
            lastName = tokens.nextToken();
        }
        else {
            System.out.println("That was not a valid input");
            return;
        }
    }
    public Name(Name name) {
        if(name != null) {
            firstName = name.firstName;
            middleName = name.middleName;
            lastName = name.lastName;   
        }
    }

当我运行它时,我在行Supervisor.workerName = supervisor.workerName;上收到错误。我理解阅读答案https://stackoverflow.com/a/10464598/5339899时的错误,但我不明白为什么supervisor.workerName为空。

我删除了我认为与问题无关的类中的一些代码,所以如果它似乎缺少重要的东西请注释,我将使用所需的代码更新

3 个答案:

答案 0 :(得分:1)

你永远不会在类中初始化你的Supervisor变量,所以你得到空指针异常。

还有一件事 - 常见的惯例是类名以大写开头,变量以小写开头,因此Supervisor作为变量的名称并不是那么好。也不建议使用两个具有相同名称的变量,一个使用大写,一个使用小写,这使得像这样的行非常混乱:Supervisor.workerName = supervisor.workerName;

答案 1 :(得分:1)

您还没有实例化Supervisor字段,因此无法调用Supervisor.workerName。

您还将该字段声明为私有字段:

private Name workerName;

这意味着,一旦您实例化了“工人主管”字段,您就无法访问它。

一个好的做法是使用getter方法从字段中检索值而不是直接访问它。

public Name getWorkerName(){
      return workerName;
}

然后你将它用作:     supervisor.getWorkerName()

另外,java中的另一个好习惯是用小写字母启动所有变量。我花了一分钟阅读您的代码,了解Supervisor是变量的名称,而不是对象调用。

这会更好:

private Worker supervisor;

答案 2 :(得分:0)

例外的问题如下:

Supervisor.workerName = supervisor.workerName;

此语句的右侧是正确的,因为您正在验证supervisor.workerName,但左侧实际上是类worker的实例,并且从未调用构造函数。 ERGO NullPointer ....