Variable might not have been initialized
RESOLVED: So I have this code that returns the highest payed employee. But it wont compile and I get the error message, 'variable employee might not have been initialized'.
Code :
/**
* Locates and returns the employee that earned the most
* @return Employee object that earned the most
*/
public Employee highestPayedEmployee()
{
double leastAmount = 0.0;
Employee employee;
for(Employee e : employeeList){
if(leastAmount <= e.calculateMonthlyPay()){
leastAmount = e.calculateMonthlyPay();
employee = e;
}
}
return employee;
}
So then I tried putting the definition of that variable in the if statement, but I get the error, cannot find symbol - variable employee. I'm guessing because since its in the if statement it may not actually reach it. So I just tried putting it outside the if statement in the for loop, like this for testing purposes:
Code :
public Employee highestPayedEmployee()
{
double leastAmount = 0.0;
// Employee employee;
for(Employee e : employeeList){
if(leastAmount <= e.calculateMonthlyPay()){
leastAmount = e.calculateMonthlyPay();
}
Employee employee = e;
}
return employee;
}
Then I get that error again, cannot find symbol - variable employee.
So the only option to make this work at all was to do this..
Code :
public Employee highestPayedEmployee()
{
if(!employeeList.isEmpty()){
double leastAmount = 0.0;
Employee employee = employeeList.get(0);
for(Employee e : employeeList){
if(leastAmount <= e.calculateMonthlyPay()){
leastAmount = e.calculateMonthlyPay();
employee = e;
}
}
}
return employee;
}
Is this the correct way to be doing this? It seems an odd way to do it, I got stuck on a test trying to figure this out!
Thanks :)
Re: Variable might not have been initialized
Stick with your original code. The error is happening because local variables do not have a default value. So what should the method return if employeeList is empty?
Re: Variable might not have been initialized
Quote:
Originally Posted by
KevinWorkman
Stick with your original code. The error is happening because local variables do not have a default value. So what should the method return if employeeList is empty?
It won't actually compile if I use that code. Or the second code.
Re: Variable might not have been initialized
Quote:
Originally Posted by
JavaGirl9001
It won't actually compile if I use that code. Or the second code.
I know. I'm telling you what the error message means. I actually don't think any of your code compiles.
Re: Variable might not have been initialized
Quote:
Originally Posted by
KevinWorkman
I know. I'm telling you what the error message means. I actually don't think any of your code compiles.
Shouldn't it return null? and yes the last one works correctly
Re: Variable might not have been initialized
Quote:
Originally Posted by
JavaGirl9001
Shouldn't it return null?
No, like I said, local variables do not have a default value, including null. If you want the default value to be null, you have to set it as such.
Quote:
Originally Posted by
JavaGirl9001
and yes the last one works correctly
If that code works correctly, then you have another variable named employee that is outside of the code you posted. Chances are that's not what you want.
Re: Variable might not have been initialized
Thanks!
I set it to null and now it works. I didn't know they didn't have default values, I was always wondering why I got these funny little errors.
Thank you :)
Re: Variable might not have been initialized
It's a pretty common mistake. For more information, default values (or lack thereof) are covered in the basic tutorials: Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)