Error java.lang.NullPointerException while calling class method -
im attempting make program takes names, dates of start, salary of employees. ive made several classes including 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 " + 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 trying assign supervisor 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 not valid input"); return; } } public name(name name) { if(name != null) { firstname = name.firstname; middlename = name.middlename; lastname = name.lastname; } }
when run error on line supervisor.workername = supervisor.workername;
. understand error reading answer https://stackoverflow.com/a/10464598/5339899 cant understand why supervisor.workername
null.
i trimmed out of code in classes felt irrelevant question if seems missing important please comment , update desired code
you never initialize supervisor
variable in class, you're getting null pointer exception.
one more thing- common convention class name starts uppercase , variables lowercase, supervisor name variable not good. not recommended have 2 variables same name, 1 uppercase , 1 lower case, makes lines 1 confusing: supervisor.workername = supervisor.workername;
Comments
Post a Comment