6 Replies - 2055 Views - Last Post: 09 January 2010 - 09:03 AM Rate Topic: -----

#1 oceanus  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 05-January 10

Why does it say I'm missing a return statement?

Posted 09 January 2010 - 08:48 AM

What can I do to fix this? It says I'm missing a return statement, when I put one...

 public Student search(int find)
	  {
		 for(Student i: StudentBody)
		 {
			if(i.studentID == find)
			   return i;
			else{ 
			   return null; }
		 }
	  }



The compiler throws this error: StudentBody.java:50: missing return statement
Is This A Good Question/Topic? 0
  • +

Replies To: Why does it say I'm missing a return statement?

#2 chili5  Icon User is offline

  • D.I.C Lover

Reputation: 20
  • View blog
  • Posts: 1,144
  • Joined: 28-December 07

Re: Why does it say I'm missing a return statement?

Posted 09 January 2010 - 08:50 AM

You get that message because you are missing a return statement. :P

More seriously, you have a return statement in the loop structure but if the loop never enters there is no return statement.

You need a return statement just after your loop.

for(Student i: StudentBody)
		 {
			if(i.studentID == find)
			   return i;
			else{
			   return null; }
		 }
return null; // return statement after the loop in case the loop never enters


Was This Post Helpful? 0
  • +
  • -

#3 oceanus  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 05-January 10

Re: Why does it say I'm missing a return statement?

Posted 09 January 2010 - 08:54 AM

View Postchili5, on 9 Jan, 2010 - 07:50 AM, said:

You get that message because you are missing a return statement. :P

More seriously, you have a return statement in the loop structure but if the loop never enters there is no return statement.

You need a return statement just after your loop.

for(Student i: StudentBody)
		 {
			if(i.studentID == find)
			   return i;
			else{
			   return null; }
		 }
return null; // return statement after the loop in case the loop never enters



well do you know how I can fix this? How can I just throw in another return statement without messing up the function of the method?
Was This Post Helpful? 0
  • +
  • -

#4 chili5  Icon User is offline

  • D.I.C Lover

Reputation: 20
  • View blog
  • Posts: 1,144
  • Joined: 28-December 07

Re: Why does it say I'm missing a return statement?

Posted 09 January 2010 - 08:56 AM

I just told you how to fix it. You need a return statement outside of the loop. The program may never reach that point but it has to be there.

 public Student search(int find)
	  {
		 for(Student i: StudentBody)
		 {
			if(i.studentID == find)
			   return i;
			else{
			   return null; }
		 }
return null;
	  }


Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Why does it say I'm missing a return statement?

Posted 09 January 2010 - 08:57 AM

To expand on what chili5 is saying, let's brainstorm alright? It says that there is a missing return statement but you have two. However, they are inside the foreach loop. NOW, what if StudentBody is empty, and have no Strings to loop through? It never enters anything inside the for loop and misses both returns.

So, I would return null and on the receiving end, if the answer is null, do something else. Just like chili5 said.
Was This Post Helpful? 0
  • +
  • -

#6 oceanus  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 05-January 10

Re: Why does it say I'm missing a return statement?

Posted 09 January 2010 - 09:00 AM

View PostDogstopper, on 9 Jan, 2010 - 07:57 AM, said:

To expand on what chili5 is saying, let's brainstorm alright? It says that there is a missing return statement but you have two. However, they are inside the foreach loop. NOW, what if StudentBody is empty, and have no Strings to loop through? It never enters anything inside the for loop and misses both returns.

So, I would return null and on the receiving end, if the answer is null, do something else. Just like chili5 said.


I fixed it, what I did was take out the else part and put return null out of the for loop. that way if it can't find anything in the array, it will automatically return null and this also fixes the return statement error. thanks anyways guys
Was This Post Helpful? 0
  • +
  • -

#7 chili5  Icon User is offline

  • D.I.C Lover

Reputation: 20
  • View blog
  • Posts: 1,144
  • Joined: 28-December 07

Re: Why does it say I'm missing a return statement?

Posted 09 January 2010 - 09:03 AM

Good. The method you had before really made no sense anyways.

 public Student search(int find)
	  {
		 for(Student i: StudentBody)
		 {
			if(i.studentID == find)
			   return i;
			else{
			   return null; }
		 }
return null;
	  }



If the first student doesn't match the ID then you are returning null and saying it doesn't exist. Thus, you are always comparing just the first item in your array/list/ whatever structure it is.

Glad you figured it out!

To save this problem in the future always have a return statement just before the end of the method (even if you know logically it will never occur).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1