Object to String总是给我null

时间:2013-01-31 03:07:26

标签: java tostring

基本问题,我已尝试应用其他几个相关问题来解决这个问题,但这总是回归为空。

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;
}

}

为什么我尝试在任何地方使用它作为打印输出,我看到的只有null,null null。我试过多次调整代码无济于事。

以下是成功运行的测试类片段,只返回名称的空值:

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());

这也是Name类:

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();
}

}

2 个答案:

答案 0 :(得分:2)

您的Consultant类有一个构造函数,它将Name作为参数。

实例化Consultant时,您必须传入Name firstNamemiddleNamelastName为空的Consultant对象。

我建议您在新建Name对象之前在一行上标记调试器并检查传入的Name.firstName对象,看看Consultant是否有任何值,等

我还会在firstName的构造函数中添加断点,以观察middleNamelastNameName的值。

<强>更新

您刚刚发布了public Name(String lastName, String firstName){ } 的代码,您的构造函数什么也没做。您的代码为:

public Name(String lastName, String firstName){
    this.lastName = lastName;
    this.firstName = firstName;
}

应该阅读

{{1}}

答案 1 :(得分:0)

您将名称初始化为:

Name contact = new Name(lName, fName);

但是你没有做任何事情

public Name(String lastName, String firstName){

}

并在您的Name课程中

private String firstName;
private String middleName;
private String lastName;

全部初始化为null s。所以当你打电话时

public void setFirstName(){
    firstName = name.getFirstName();
}

public void setMiddleName(){
    middleName = name.getMiddleName();
}

public void setLastName(){
    lastName = name.getLastName();
}

Consultant课程中,他们获得全部null