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 6 of 6

Thread: Missing return statement???

  1. #1
    Junior Member
    Join Date
    May 2013
    Location
    Wellington, NZ.
    Posts
    11
    My Mood
    Cold
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Missing return statement???

    Hello everyone,

    Forgive me for any errors in posting this here or simply bad posting syntax,
    this is my first time posting here and probably won't be my last.

    public Person getPerson(String name){
            for(int i = 0;  i< this.db.size(); i++){
                Person temp = this.db.get(i);
                String tempName = temp.getName();
                if(tempName.equals(name)){
                    return temp;
                }
                else{
                    return null;
                }
            }
        }

    The compiler BlueJ keeps telling me there is a missing return statement and I don't know why as there are two return statements in the two if branches.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Missing return statement???

    Find the third possible branch

  3. #3
    Junior Member
    Join Date
    May 2013
    Location
    Wellington, NZ.
    Posts
    11
    My Mood
    Cold
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Missing return statement???

    Quote Originally Posted by jps View Post
    Find the third possible branch
    Thanks for the reply turns out I just had to shift my:

    "return null;"

    a couple of curly brackets down

  4. #4
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Missing return statement???

    Just so you know, your for loop will only ever cycle through one time.

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Missing return statement???

    Quote Originally Posted by Parranoia View Post
    Just so you know, your for loop will only ever cycle through one time.
    Fixing the compile error (correctly) also corrected that issue as per post #3

  6. #6
    Junior Member
    Join Date
    May 2013
    Location
    Wellington, NZ.
    Posts
    11
    My Mood
    Cold
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Missing return statement???

    Yeah the issue was my 'return null;' had top be outside of the for loop which is now fixed

Similar Threads

  1. missing return statement need help!!
    By widowx in forum What's Wrong With My Code?
    Replies: 32
    Last Post: March 29th, 2013, 01:41 PM
  2. Missing return statement
    By beerye28 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 23rd, 2013, 06:48 PM
  3. Missing return statement
    By mrroberts2u in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2011, 06:11 AM
  4. Missing return statement
    By Stn in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 1st, 2011, 08:03 PM
  5. Error of "Class has no return statement"
    By mdstrauss in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 2nd, 2009, 12:00 PM

Tags for this Thread