Home > Software engineering >  "Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Er
"Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Er

Time:01-18

This is my main() class

    /**
     * @param args the command line arguments
     * @throws java.lang.Exception
     */
    public static void main(String[] args) {
        // TODO code application logic here
        //create array
        Person persons[] = new Person[3];
        System.out.println("=====Management Person programer=====");        
        
        Person p = new Person(); 
        //repeat 3 times
        for (int i =0; i< 3 ;i  ){           
           persons[i] = p.inputPersonInfo("name","address",2.1);
        }
}

And this is my Person() class:

    private String name;
    private String address;
    private double salary;
    private static Scanner in = new Scanner(System.in);    
    //Person persons[] = new Person[100];
    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 String getAddress() {
        return address;
    }

    public double getSalary() {
        return salary;
    }

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

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

    public void setSalary(double salary) {
        this.salary = salary;
    }
    
    // accept input from keyboard
    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 = checkInputString();
        System.out.println("Please input address");
        address = checkInputString();
        System.out.println("Please input salary");
        salary = checkInputSalary();        
        return new Person(name,address,salary);
        
        }
    //display person info
    public void displayPersonInfo(Person persons) {
        System.out.println("Information of Person you entered");
        System.out.println("Name: "   this.name);
        System.out.println("Address: "   this.address);
        System.out.println("Salary: "   this.salary);    
    }
}

i am trying to make a program to input and display array of persons(maximum of 3), you can input each person name, address, salary

i dont understand that in the main class when i call the inputPersonInfo(), i write like this and it didn't let me enter the first person:

persons[i] = p.inputPersonInfo(name,address,salary);

it keep showing error "Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: ".

when i hover my cursor into the error code it is say:"can not find symbol name,address,salary".

but when i put name and address in " " and change salary into a random number and it work, it even let me enter all 3 persons and display all of them no problem:

persons[i] = p.inputPersonInfo("name","address",2.1);

What are the above code suppose to do and why putting name and address in "" and change salary into random number it work like normal?

CodePudding user response:

Some little mistakes cause problem in your code. You have to pass getter as argument while you call inputPersonInfo(person.getName(), person.getAddress(), person.getSalary()) mehtod in main class. In displayPersonInfo() method you have to pass data as getter.

Here down is modified code:

import java.util.*;
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 String getAddress() {
        return address;
    }

    public double getSalary() {
        return salary;
    }

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

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

    public void setSalary(double salary) {
        this.salary = salary;
    }
    
   
    public Person inputPersonInfo (String name, String address, Double salary) throws Exception{       
        Scanner sc = new Scanner(System.in);
        
        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();
        
        if(salary <= 0)
        {
            throw new Exception("Salary "   salary   " is invalid, enter valid salary");
        }
        
        return new Person(name,address,salary);
        
    }
    
    public void displayPersonInfo(Person persons) {
        
        System.out.println("Name: "   persons.getName());
        System.out.println("Address: "   persons.getAddress());
        System.out.println("Salary: "   persons.getSalary());    
    }
}    

public class Main
{
    public static void main(String[] args) {        
        int count = 1;
        
        // Array of person
        Person persons[] = new Person[3];
        
        // No argument constructor
        Person person = new Person();
         
        System.out.println("===== Insert Person =====");
        for (int i = 0; i < 3 ; i  ){      
            System.out.println("Input Information of Person "   (i   1));
            System.out.println("----------------------------------");
            try {
                persons[i] = person.inputPersonInfo(person.getName(), person.getAddress(), person.getSalary());
            } catch(Exception e) {
                System.out.print(e);    
                System.exit(0);
            }
            System.out.println();
        }
        
        
        System.out.println("===== Person List =====");
        for (Person p: persons){    
            System.out.println("Information of Person "   (count  )   " you entered");
            System.out.println("----------------------------------");
            person.displayPersonInfo(p);
            System.out.println();
        }
    }
}

OUTPUT

===== Insert Person =====
Input Information of Person 1
----------------------------------
===== Insert Person =====
Input Information of Person 1
----------------------------------
Please input name
Jack
Please input address
California
Please input salary
90000

Input Information of Person 2
----------------------------------
Please input name
Michael
Please input address
Michigan
Please input salary
80000

Input Information of Person 3
----------------------------------
Please input name
David
Please input address
Nevada
Please input salary
70000

===== Person List =====
Information of Person 1 you entered
----------------------------------
Name: Jack
Address: California
Salary: 90000.0

Information of Person 2 you entered
----------------------------------
Name: Michael
Address: Michigan
Salary: 80000.0

Information of Person 3 you entered
----------------------------------
Name: David
Address: Nevada
Salary: 70000.0
  •  Tags:  
  • Related