简体   繁体   中英

Object returns null

Summary: New to Java, tried looking through other posts but didn't find an answer. I'm learning inheritance and have an AddressBook class extended by a Runner class. When I write a program to test the inheritance I create a Runner object. If I get the first String parameter it returns fine but when I attempt to get the second String parameter it returns null.

Question: Why is the second parameter returning null?

 package Assignment_1; //Begin Class Definition public class AddressBook { // Member variables private String businessPhone; private String cellPhone; private String facebookId; private String firstName; private String homeAddress; private String homePhone; private String lastName; private String middleName; private String personalWebSite; private String skypeId; //Constructors public AddressBook (String firstName, String middleName, String lastName, String homeAddress, String businessPhone, String homePhone, String cellPhone, String skypeId, String facebookId, String personalWebSite) { this.firstName = firstName; this.middleName = middleName; this.lastName = lastName; this.homeAddress = homeAddress; this.businessPhone = businessPhone; this.homePhone = homePhone; this.cellPhone = cellPhone; this.skypeId = skypeId; this.facebookId = facebookId; this.personalWebSite = personalWebSite; } public AddressBook (String firstName) { this.firstName = firstName; } public AddressBook(String firstName, String middleName) { this.firstName = firstName; this.middleName = middleName; } public AddressBook (String firstName, String middleName, String lastName) { this.firstName = firstName; this.middleName = middleName; this.lastName = lastName; } // Getters and setters public String getFirstName() { return firstName; } public String getMiddleName() { return middleName; } public String getLastName() { return lastName; } public String getHomeAddress() { return homeAddress; } public String getBusinessPhone() { return businessPhone; } public String getHomePhone() { return homePhone; } public String getCellPhone() { return cellPhone; } public String getSkypeId() { return skypeId; } public String getFacebookId() { return facebookId; } public String getPersonalWebsite() { return personalWebSite; } public void setFirstName(String firstName) { this.firstName = firstName; } public void setMiddleName(String middleName) { this.middleName = middleName; } public void setLastName(String lastName) { this.lastName = lastName; } public void setHomeAddress(String homeAddress) { this.homeAddress = homeAddress; } public void setBusinessPhone(String businessPhone) { this.businessPhone = businessPhone; } public void setHomePhone(String homePhone) { this.homePhone = homePhone; } public void setCellPhone(String cellPhone) { this.cellPhone = cellPhone; } public void setSkypeId(String skypeId) { this.skypeId = skypeId; } public void setFacebookId(String facebookId) { this.facebookId = facebookId; } public void setPersonalWebSite(String personalWebSite) { this.personalWebSite = personalWebSite; } // Public methods public static void compareNames(String name1, String name2) { if(name1.equals(name2)) { System.out.println(name1); System.out.println(name2); System.out.println("The names are the same."); } else { System.out.println(name1); System.out.println(name2); System.out.println("The names appear to be different."); } } ************************************************************ package Assignment_1; public class BanffMarathonRunner extends AddressBook { // Member variables private int time; private int years; // Constructors public BanffMarathonRunner(String firstName, String lastName, int min, int yr) { super(firstName, lastName); time = min; years = yr; } // Getters and Setters public int getTime() { return time; } public void setTime(int time) { this.time = time; } public int getYears() { return years; } public void setYears(int years) { this.years = years; } } ************************************************************ package Assignment_1; import Assignment_1.BanffMarathonRunner; public class TestBanffMarathonRunner { public static void main(String[] args) { BanffMarathonRunner r1 = new BanffMarathonRunner("Elena", "Brandon", 341, 1); System.out.print(r1.getLastName()); } } } 

Because lastName is null.

You are calling AddressBook(String firstName, String middleName)

and setting the middleName , not the lastName .

BanffMarathonRunner r1 = new BanffMarathonRunner("Elena", "Brandon", 341, 1);

calls:

// firstName = "Elena"
// lastName = "Brandon"
// min = 341
// yr = 1
public BanffMarathonRunner(String firstName, String lastName, int min, int yr) {
    super(firstName, lastName);
    // ...
}

which calls via super(...) :

// firstName = "Elena"
// middleName = "Brandon" <-- here is your issue
public AddressBook(String firstName, String middleName) {
    this.firstName = firstName;
    this.middleName = middleName;
}

Brandon is set in AddressBook#middleName instead of AddressBook#lastName .

Your problem is in the BanffMarathonRunner.java:

in the constructor when you are calling the

super(firstName, lastName);

Actually by the call above the super class constructor with two parameter is being called, and that constructor is the one which set the middleName not the lastName.

I think you are confused because of the lastName variable name, which is passed to the constructor with two argument and that constructor use the second argument to set the middleName.

Good Luck.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM