Fundamental question, and I've tried to apply several other related questions to figure it out, but this always comes back as null.
public class Consultant {
public Name name;
String lastName;
String firstName;
String middleName;
public Consultant (Name name){
this.name = name;
this.lastName = name.getLastName();
this.firstName = name.getFirstName();
this.middleName = name.getMiddleName();
}
public Name getName(){
return name;
}
public void setFirstName(){
firstName = name.getFirstName();
}
public void setMiddleName(){
middleName = name.getMiddleName();
}
public void setLastName(){
lastName = name.getLastName();
}
@Override
public String toString(){
return lastName + ", " + firstName + " " + middleName;
}
}
Why I try to use this anywhere as a print out, all I see is null, null null. I've tried adjusting the code several times to no avail.
Here is the test class snippet that runs successfully, just returns null values for the name:
public void consultantTimeToStringTest() {
String fName = "Test";
String lName = "Dude";
Name contact = new Name(lName, fName);
Consultant testConsultant = new Consultant(contact);
System.out.println(testConsultant.toString());
Here is the Name class as well:
public class Name {
private String firstName;
private String middleName;
private String lastName;
public Name(){
}
public Name(String lastName, String firstName){
}
public Name(String lastName, String firstName, String middleName){
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Name other = (Name) obj;
if (firstName == null) {
if (other.firstName != null)
return false;
} else if (!firstName.equals(other.firstName))
return false;
if (lastName == null) {
if (other.lastName != null)
return false;
} else if (!lastName.equals(other.lastName))
return false;
if (middleName == null) {
if (other.middleName != null)
return false;
} else if (!middleName.equals(other.middleName))
return false;
return true;
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public String getMiddleName(){
return middleName;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((firstName == null) ? 0 : firstName.hashCode());
result = prime * result
+ ((lastName == null) ? 0 : lastName.hashCode());
result = prime * result
+ ((middleName == null) ? 0 : middleName.hashCode());
return result;
}
public void setFirstName(String firstName){
}
public void setLastName(String lastName){
}
public void setMiddleName(String middleName){
}
public String toString(){
StringBuilder result = new StringBuilder();
String NEW_LINE = System.getProperty("line.separator");
result.append(lastName);
return result.toString();
}
}
Your Consultant
class has a single constructor that takes in Name
as a parameter.
When you instantiate Consultant
, you must be passing in a Name
object that has null for firstName
, middleName
, lastName
.
I would recommend tagging the debugger on a line right before you new-up a Consultant
object and check the Name
object passed in and see if there are any values for Name.firstName
, etc.
I would also add a breakpoint in the constructor of Consultant
to watch the values for firstName
, middleName
, lastName
be set.
UPDATE
You just posted the code for Name
and your constructor is doing nothing. Your code reads:
public Name(String lastName, String firstName){
}
It should read
public Name(String lastName, String firstName){
this.lastName = lastName;
this.firstName = firstName;
}
You initialize name as:
Name contact = new Name(lName, fName);
But you didn't do anything in
public Name(String lastName, String firstName){
}
and in your Name
class,
private String firstName;
private String middleName;
private String lastName;
are all initialized as null
s. So when you call
public void setFirstName(){
firstName = name.getFirstName();
}
public void setMiddleName(){
middleName = name.getMiddleName();
}
public void setLastName(){
lastName = name.getLastName();
}
in your Consultant
class, they get all null
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.