Results 1 to 7 of 7
Thread: Missing Return statement =[
- 02-12-2011, 06:38 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
Missing Return statement =[
hey i get the following compile error, and im not sure what to do to fix it.
src\server\models\players\Playerassistant.java:167 2: missing return statement
}
^
this is the code.
public String getSkillName(int skillId) {
switch (skillId) {
case 0:
return "Attack";
case 1:
return "Defence";
case 2:
return "Strength";
case 3:
return "Hitpoints";
case 4:
return "Ranged";
case 5:
return "Prayer";
case 6:
return "Magic";
case 7:
return "Cooking";
case 8:
return "Woodcutting";
case 9:
return "Fletching";
case 10:
return "Fishing";
case 11:
return "Firemaking";
case 12:
return "Crafting";
case 13:
return "Smithing";
case 14:
return "Mining";
case 15:
return "Herblore";
case 16:
return "Agility";
case 17:
return "Theiving";
case 18:
return "Slayer";
case 19:
return "Runecrafting";
}
}
line 1672 is the first bracket.
Thanks, any help appreciated.
- 02-12-2011, 06:47 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
The compiler demands that the method returns a String for any value that skillId might have.
If it is your intent that skillId only have values 0-19 then you could return some bogus value or, better, throw an exception. The best thing would be to use something other than int as the type of skillId: this is what enums are for.
- 02-12-2011, 07:59 AM #3
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
Try to use break; in your code
- 02-12-2011, 08:01 AM #4
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
And finaly
- 02-12-2011, 08:17 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
if i put a number, i get imcompatible types, found: int, if i put a name i get cannot find symbol xxxx variable :S
- 02-12-2011, 08:23 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,605
- Blog Entries
- 7
- Rep Power
- 17
That would be a very silly solution; the OPs example is equivalent to this code:
Even if everybody swears that method foo only will be called with a parameter 1, 2 or 3 the compiler won't believe them because (obviously) method foo doesn't return a String value for any parameter value not in the range 1, 2 or 3. Even more, this code:Java Code:String foo(int i) { switch (i) { case 1: return "one"; case 2: return "two"; case 3: return "three"; } }
... doesn't make the compiler shut its mouth, i.e. it still thinks that at the end of the method no String value is returned. The compiler doesn't interpret the code, nor can't it understand the semantics, the intention, of the code. Basically, when the compiler whines about "no return value" it is right and rearrange your code.Java Code:String foo(int i) { if (i < 1 || i > 3) return "huh?"; switch (i) { case 1: return "one"; case 2: return "two"; case 3: return "three"; } }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-12-2011, 10:34 AM #7
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
Similar Threads
-
Missing return statement
By gkoef in forum New To JavaReplies: 8Last Post: 01-01-2011, 02:52 AM -
Missing return statement error.
By Fortu in forum New To JavaReplies: 2Last Post: 12-11-2010, 09:15 PM -
missing return statement
By bayan in forum New To JavaReplies: 6Last Post: 04-26-2010, 03:15 PM -
Missing Return Statement Error
By darkblue24 in forum New To JavaReplies: 13Last Post: 02-16-2010, 08:22 PM -
Missing Return Statement error
By anilanar in forum New To JavaReplies: 2Last Post: 08-20-2009, 01:02 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks