TheQAGuy
TheQAGuy

Reputation: 494

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

Upvotes: 0

Views: 3015

Answers (3)

Brad
Brad

Reputation: 198

You haven't instantiated the Supervisor field so Supervisor.workerName cannot be called.

You're also declaring the field as private:

private Name workerName;

Meaning that once you've instantiated the Worker Supervisor field, you won't have access to it.

A good practice would be to use a getter method to retrieve the value from the field instead of accessing it directly.

public Name getWorkerName(){
      return workerName;
}

You would then use this as: supervisor.getWorkerName()

Also, another good practice in java is to start all variables with lower case. It took me a minute reading through your code to understand that Supervisor is the name of a variable and not an object call.

This would be better:

private Worker supervisor;

Upvotes: 1

The problem with the exception is the following:

Supervisor.workerName = supervisor.workerName;

the right side of this statement is ok because you are verifying the supervisor.workerName but the left side is actually an instance from the class worker and the constructor was never called. ERGO NullPointer....

Upvotes: 0

Nir Levy
Nir Levy

Reputation: 12953

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;

Upvotes: 1

Related Questions