简体   繁体   中英

Why is my program returning null,null,0 ,0?

My program is supposed to store the info submitted by the users into another class. It is then supposed to perform a small calculation to determine age. The final part is to create an instance of the secondary class in the Driver and then print out all the info using toString. My results are null, null, 0 ,0 everytime. What am I doing wrong?

public class CustomerInfoProgram {

    public static Scanner scan = new Scanner(System.in);
    public static String firstName;
    public static String lastName;
    public static String address;
    public static String phoneNumber;
    public static int dob;
    public static int currentYear;
    public static int age;
    public static String name;

    public static void main(String[] args) {

        Customer in = new Customer();

        getCustomerName();
        getPhone();
        getAddress();
        getDOB();
        getCurrentYear();
        in.toString();

    }

    public static String getCustomerName() {

        System.out.println("What is your first name?");
        firstName = scan.nextLine();

        System.out.println("What is your last name?");
        lastName = scan.nextLine();

        name = firstName + " " + lastName;

        return name;

    }

    public static String getAddress() {

        System.out.println("What is your address?");
        address = scan.nextLine();

        return address;
    }

    public static String getPhone() {

        System.out.println("What is your phone number?");
        phoneNumber = scan.nextLine();

        return phoneNumber;
    }

    public static int getDOB() {

        System.out.println("What year were you born?");
        dob = scan.nextInt();

        return dob;
    }

    public static int getCurrentYear() {

        System.out.println("What is the current year?");
        currentYear = scan.nextInt();

        return currentYear;
    }
}

Secondary class:

import java.util.Scanner;

public class Customer {

    public static int age;
    public static String allInfo;
    public static String name = CustomerInfoProgram.name;
    public static String address = CustomerInfoProgram.address;
    public static String phoneNumber = CustomerInfoProgram.phoneNumber;
    public static int dob = CustomerInfoProgram.dob;
    public static int currentYear = CustomerInfoProgram.currentYear;

    private int getAge() {

        age = (currentYear - dob);

        return age;
    }

    public String toString() {

        getAge();
        allInfo = (name + " " + address + " " + phoneNumber + " " + dob + " " + age);
        System.out.println(allInfo);

        return toString();
    }
}
public static String name = CustomerInfoProgram.name;

This only sets the value of CustomerInfoProgram.name when the field is initialized, ie once, when the Customer class is loaded - it does not update when CustomerInfoProgram.name is updated.

The Customer class is (at the latest) loaded when you call new Customer() , which happens before you run any methods to set the value of CustomerInfoProgram.name , so it is null when you attempt to use name .

If you want to use the current value of CustomerInfoProgram.name , just refer to that field directly. Alternatively, you can add a getter, which will return its current value:

public static String getName() {
  return CustomerInfoProgram.name;
}

then call the getter in place of name :

allInfo = (getName() + " " + ...

(Exactly the same applies to the other fields).


You also will get a StackOverflowError from you toString() method:

public String toString() {

    getAge();
    allInfo = (name + " " + address + " " + phoneNumber + " " + dob + " " + age);
    System.out.println(allInfo);

    return toString();
}

You are unconditionally calling toString() inside toString() , so this will just keep calling itself until it runs out of stack space.

Instead, just return allInfo :

public String toString() {

    getAge();  // This actually does nothing - do you intend to use it in the return value?
    return (name + " " + address + " " + phoneNumber + " " + dob + " " + age);
}

and call System.out.println in your main method:

System.out.println(in); // Implicitly calls toString().

See @Andy's answer for the cause of your problem.

The usual approach to this would be to collect the values to be stored in your Customer object before you create it, then use a nondefault constructor call. A partial example might look like this:

// get individual values here
getCustomerName();
getPhone();
// etcetera

Customer in = new Customer(name, phone, address, dob);

The rest I leave as an exercise for the reader.

Being static attributes the initialization in both the classes would be only once. So post you get the input from user those values are not set to the attributes. Hence the 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.

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