Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Variable might not have been initialized

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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'.

        /**
         * 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:


        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..

        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
    Last edited by JavaGirl9001; December 7th, 2011 at 11:19 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Variable might not have been initialized

    Quote Originally Posted by KevinWorkman View Post
    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.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Variable might not have been initialized

    Quote Originally Posted by JavaGirl9001 View Post
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Variable might not have been initialized

    Quote Originally Posted by KevinWorkman View Post
    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

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Variable might not have been initialized

    Quote Originally Posted by JavaGirl9001 View Post
    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 View Post
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. variable might not be initialized problem. Can someone tell me whats wrong?
    By NewAtJava in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 10th, 2011, 09:25 AM
  2. Replies: 4
    Last Post: October 20th, 2011, 11:26 AM
  3. Conditions and 'might not have been initialized' error.
    By mwebb in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 2nd, 2011, 11:22 AM
  4. variable might not have been initialized
    By SV25 in forum Java Theory & Questions
    Replies: 1
    Last Post: April 25th, 2011, 10:58 AM
  5. Variable might not have been initialized?
    By n56 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 30th, 2010, 03:03 PM