javaclass

Error java.lang.NullPointerException while calling class method


Im attempting to make a program that takes the names, dates of start, salary of employees. So ive made several classes including my calling 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;   
        }
    }

When i run it i get the error on the line Supervisor.workerName = supervisor.workerName;. I understand the error from reading the answer https://stackoverflow.com/a/10464598/5339899 but i cant understand why supervisor.workerName is null.

I trimmed out some of the code in the classes that i felt was irrelevant to the question so if it seems to be missing something important please comment and i will update with the desired code


Solution

  • you never initialize your Supervisor variable in the class, so you're getting null pointer exception.

    one more thing- the common convention is that class name starts with uppercase and variables with lowercase, so Supervisor as a name for a variable is not that good. it is also not recommended to have two variables with the same name, one with uppercase and one with lower case, it makes lines like this one very confusing: Supervisor.workerName = supervisor.workerName;