Burt Reynolds Burt Reynolds - 5 months ago 11
Java Question

Reading a File, and instantiating an object with properties based on the data read

I am trying to import data from a file and create objects out of it and then store it into an array. I am a beginner and I am not sure what I am doing wrong.

I recieve 6 errors:


  1. .\Employees.java:92: error: class, interface, or enum expected
    }
    ^

  2. Employeetest.java:24: error: cannot find symbol
    objectArray[counter] = setFirstName(firstNameEmployee);

  3. symbol: method setFirstName(String)
    location: class Employeetest
    Employeetest.java:26: error: cannot find symbol
    objectArray[counter] = setLastName(lastNameEmployee);

  4. symbol: method setLastName(String)
    location: class Employeetest
    Employeetest.java:28: error: cannot find symbol
    objectArray[counter] = setAge(ageEmployee);

  5. symbol: method setAge(double)
    location: class Employeetest
    Employeetest.java:30: error: cannot find symbol
    objectArray[counter] = setHourlySalary(hourlySalaryEmployee);

  6. symbol: method setHourlySalary(double)
    location: class Employeetest
    Employeetest.java:32: error: cannot find symbol
    objectArray[counter] = setWeeklyHoursWorked(weeklyhoursworkedEmployee);

    symbol: method setWeeklyHoursWorked(double)
    location: class Employeetest
    6 errors



This is what I have.

This is my objects class
public class Employees {

private String firstName;
private String lastName;
private double age;
private double hourlySalary;
private double weeklyhoursworked;
private double weeklysalary;

public void setFirstName(String EmployeeFirstName) {
firstName = EmployeeFirstName;
return;
}
public void setLastName(String EmployeeLastName){
lastName = EmployeeLastName;
return;
}
public void setAge(double EmployeeAge){
age = EmployeeAge;
return;
}
public void setHourlySalary(double EmployeeHourlySalay){
hourlySalary = EmployeeHourlySalay;
calculateWeeklySalary(weeklyhoursworked);
return;
}
public void setWeeklyHoursWorked(double EmployeeWeeklyHoursWorked){
weeklyhoursworked = EmployeeWeeklyHoursWorked;
return;
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public double getAge(){
return age;
}
public double getHourlySalary(){
return hourlySalary;
}
public double getWeeklyHourlyWorked(){
return weeklyhoursworked;
}
public double getWeeklySalary(){
return weeklysalary;
}
public void calculateWeeklySalary(double hoursWorked) {
weeklysalary = (hoursWorked * hourlySalary);
return;
}
}


This is my main program where I am trying to import the data, convert it to an object, then it store it as an array.

import java.io.*;

public class Employeetest
{


public static void main(String[] args) throws Exception
{

String firstNameEmployee = "null";
String lastNameEmployee = "null";
double ageEmployee;
double hourlySalaryEmployee;
double weeklyhoursworkedEmployee;
double weeklySalaryEarnedEmployee;
int counter = 0;

Employees objectArray[] = new Employees[counter + 1];
FileReader fr = new FileReader("Employeedata.txt");
BufferedReader br = new BufferedReader(fr);

counter = 0;
while ((firstNameEmployee = br.readLine()) != null) {
objectArray[counter] = setFirstName(firstNameEmployee);
lastNameEmployee = br.readLine();
objectArray[counter] = setLastName(lastNameEmployee);
ageEmployee = Double.parseDouble(br.readLine());
objectArray[counter] = setAge(ageEmployee);
hourlySalaryEmployee = Double.parseDouble(br.readLine());
objectArray[counter] = setHourlySalary(hourlySalaryEmployee);
weeklyhoursworkedEmployee = Double.parseDouble(br.readLine());
objectArray[counter] = setWeeklyHoursWorked(weeklyhoursworkedEmployee);
System.out.println(objectArray[counter]);
counter++;
}
br.close();
System.exit(0);
}
}


Unidentifiable Error, Anything noticeable?

Answer

Problem

You are calling methods that cannot be accessed without accessing the object first..

Moving On:

while ((firstNameEmployee = br.readLine()) != null) {
    objectArray[counter] = setFirstName(firstNameEmployee);
    lastNameEmployee = br.readLine();
    objectArray[counter] = setLastName(lastNameEmployee);
    ageEmployee = Double.parseDouble(br.readLine());
    objectArray[counter] = setAge(ageEmployee);
    hourlySalaryEmployee = Double.parseDouble(br.readLine());
    objectArray[counter] = setHourlySalary(hourlySalaryEmployee);
    weeklyhoursworkedEmployee = Double.parseDouble(br.readLine());
    objectArray[counter] = setWeeklyHoursWorked(weeklyhoursworkedEmployee);
    System.out.println(objectArray[counter]);
counter++;
}

We are going to take a look at this code block. First, you read a value from the file and assign it to a variable. Then, you assigned it to the object... Which is where the problem becomes apparent

This is what you have

objectArray[counter] = setLastName(lastNameEmployee);

To Fix just change all the lines that have methods like the above to this

objectArray[counter].setLastName(lastNameEmployee);

Another Example

Instead of

 objectArray[counter] = setAge(ageEmployee);

do

 objectArray[counter].setAge(ageEmployee);

To Fix the Null Pointer Exception

for(int x =0; x< objectArray.length;x++)
{
    objectArray[x] = new Employees();
}

Put that loop right under

BufferedReader br = new BufferedReader(fr);

The reason is because objects in an object array are null at first. Thus, you need to instantiate them before u can mutate their instance variables.

Comments