1

i have this Main code

    public static void main(String[] args)
    {
        //display Gui
        DisplayGUI();
        //input Person
        InputPerson();
        //display input data
        Display();
        //sort person
        //SortbySalary();
        //display sorted person
        //Display();        
        
    }

    private static void DisplayGUI() {
        System.out.println("=====Management person Programmer=====");
    }
    
    private static void InputPerson() {
        //array of person
        Person persons[] = new Person[3];
        //constructor
        Person person = new Person();
        
        for(int i = 0; i<3; i++){
            persons[i] = person.InputPersonInfo(person.getName(),person.getAddress(),person.getSalary());
        }
        
        for (Person p: persons){
            person.DisplayPersonInfo(p);
            System.out.println();
        }        
    }

    public static void Display() {
        Person person = new Person();
        Person persons[] = null;
                for (Person p: persons){
            person.DisplayPersonInfo(p);
            System.out.println();
        } 
    }
}

this is my person class

class Person {
    private String name;
    private String address;
    private double salary;

    public Person() {
    }

    public Person(String name, String address, double salary) {
        this.name = name;
        this.address = address;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
    
    public Person InputPersonInfo(String name, String address, double salary){
        Scanner sc = new Scanner(System.in);
        System.out.println("Input Information of person:");
        System.out.println("Please input name:");
        name = sc.nextLine();
        System.out.println("Please input Address)");
        address = sc.nextLine();
        System.out.println("Please input salary:");
        salary = sc.nextDouble();
        
        return new Person(name,address,salary);
        
    }
    public void DisplayPersonInfo(Person persons){
        System.out.println("Information of Person you entered");
        System.out.println("Name:" + persons.getName());
        System.out.println("address:" + persons.getAddress());        
        System.out.println("slary:" + persons.getSalary());        
    }

i dont understand when i push this code in the InputPerson() class, it will display the information of person i entered

        for (Person p: persons){
            person.DisplayPersonInfo(p);
            System.out.println();
        } 

but when i try to do this in Display() class it keep telling error "Cannot read the array length because "" is null"

i dont know how to pass data i entered in InPutPerson() class to the Display() class, please help me

Manh Pham
  • 23
  • 1
  • 6
  • 1
    As an aside, I'd strongly advise you to follow Java naming conventions - method names like `InputPersonInfo` and `DisplayPersonInfo` are unconventional. – Jon Skeet Feb 07 '22 at 15:24

1 Answers1

0

InputPerson method should return persons and then use this output as a parameter in display(persons) method.

Something like this:

    public static void main(String[] args) {
        //input Person
        Person persons[] = InputPerson();
        //display input data
        display(persons);

    }

    private static Person[] InputPerson() {
        Person persons[] = new Person[3];
        Person person = new Person();

        for (int i = 0; i < 3; i++) {
            persons[i] = person.InputPersonInfo(person.getName(), person.getAddress(), person.getSalary());
        }

        for (Person p : persons) {
            person.displayPersonInfo(p);
            System.out.println();
        }
        return persons;
    }

    public static void display(Person[] persons) {
        Person person = new Person();
        for (Person p : persons) {
            person.displayPersonInfo(p);
            System.out.println();
        }
    }
}

  • can you explain to me what is the (Person[] persons) mean ? – Manh Pham Feb 08 '22 at 06:22
  • 1
    This means that method display() accepts an array of persons. This is what the inputPerson() method returns. What you wrote doesn't work because the persons array in the display() method is empty, this is why you want to accept it as a parameter in the display() method. – Sabina Matjašič Feb 08 '22 at 10:54