1

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

Community
  • 1
  • 1
TheQAGuy
  • 498
  • 7
  • 17
  • 2
    It isn't, but `Supervisor` is, so you can't assign a value to `Supervisor.workername`. Your null check is checking to make sure the parameter isn't null, but you aren't checking to make sure that you've ever initialized the `Supervisor` field to be a new `Worker`. – azurefrog Oct 02 '15 at 21:36
  • 1
    What @azurefrog said. Possibly what you're after is `Supervisor = supervisor`. – John Bollinger Oct 02 '15 at 21:38
  • 1
    Also, use standard naming conventions. Member names such as "Supervisor" should be written in `camelCase`. That really does make your code easier for others to read. – John Bollinger Oct 02 '15 at 21:39
  • Yes, @JohnBollinger is right. It's even confused StackOverflow's syntax highlighting. If you look at the highlighting, it thinks that `Supervisor` is a class, not a variable. – DavidS Oct 02 '15 at 21:41
  • @JohnBollinger Thanks, for this assignment we were instructed by the instructor as to what to name each variable so I guess he's not the best instructor :). But thanks ill take that naming into consideration for future projects – TheQAGuy Oct 02 '15 at 21:44

3 Answers3

1

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;

Nir Levy
  • 12,750
  • 3
  • 21
  • 38
1

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;
Brad
  • 198
  • 10
0

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....

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97