| Author |
The method must return a result type of int?
|
joseph cooper
Ranch Hand
Joined: Nov 08, 2005
Posts: 44
|
|
I am receiving an error stating that the method must return a result of type int. I am in the process of refactoring code and can not seem to get rid of this error. I have tried a few things to try and resolve the error: placed an else on the if statement that returned an integer, rewritting the contents of the catch to just return an integer, having the last statement return an integer, etc... I am not seeing the condition for this other int? Any help would be appreciated. Thanks in advance int insureHistory(StringBuffer data, int policyId, int requirementCode) throws Throwable { if(requirementCode == Codes.INSURE_HISTORY_CODE) { Section_InsHistory ih = new Section_InsHistory(); try { ih.getData(policyId, requirementCode); } catch(EricException ee) { return errorMessage(ee, data); } finally { ih = null; } } }
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9939
|
|
this isn't really an intermediate question, but... your method is declared to return an int: that means that the ONLY thing you can return is an int, and you MUST return an int. I'm not sure what errorMessage returns. it MAY return an int, but we can't tell. also, what happens if no exception is caught? what are you returning? [ December 13, 2007: Message edited by: Fred Rosenberger ]
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
joseph cooper
Ranch Hand
Joined: Nov 08, 2005
Posts: 44
|
|
|
I thought I had already addressed those questions. I tried some rewriting to have the very last statement return an integer value. I hard coded this value to 0. The error message method just prints some error messages and returns a value of -1. I just hard coded the value of -1. I figured that there might be a problem with an else needing to be inplace. This else statement was hard coded to 0. In all these conditions seperately as well as all the conditions together, I still received the integer error. Does anyone have any other ideas or sees what I am missing?
|
 |
Bill Shirley
Ranch Hand
Joined: Nov 08, 2007
Posts: 457
|
|
(Use the code markers in your posts, and try to format the code (even though the web editor doesn't handle tabs).) You didn't have the return value added in your code (as above).
|
Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9939
|
|
Every execution path has to have a return statement in the end, and has to return an int. The code you posted does not
|
 |
joseph cooper
Ranch Hand
Joined: Nov 08, 2005
Posts: 44
|
|
As I have already said twice now, I have added these return statements as seen in all caps to my initial code. These were added individually and tested with the error statement and all of them were added together and tested with the error statement. There is another condition or something else wrong with this code I am refactoring that I am not seeing. Does anyone see a problem? int insureHistory(StringBuffer data, int policyId, int requirementCode) throws Throwable { if(requirementCode == Codes.INSURE_HISTORY_CODE) { Section_InsHistory ih = new Section_InsHistory(); try { ih.getData(policyId, requirementCode); } catch(EricException ee) { RETURN -1; } finally { ih = null; } } else { RETURN -1; } RETURN 0; }
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9939
|
|
Originally posted by joseph cooper: As I have already said twice now, I have added these return statements as seen in all caps to my initial code.
1) If you don't post it here, we can't really tell what you've done. 2) does your real code use "RETURN" or "return"? It makes a difference. 3) PLEASE PLEASE PLEASE use the code tags when you post your code. it will preserve the formatting and make your code easier to read. Click that littel "code" button below where you type in your post, and then paste your source code between the tags it generates. 4) Post the EXACT TEXT of the erorr message, in it's entirety. don't paraphrase, don't post part of it. 5) "The error message method just prints some error messages and returns a value of -1." What do you mean that the error message returns -1?
|
 |
joseph cooper
Ranch Hand
Joined: Nov 08, 2005
Posts: 44
|
|
1. The exact error message is "This method must return a result of type int." 2. I did not want to list tweleve occurances of the code segments with slight changes of what I have attempted. I figured it was more readable to just say what I had already done. I reduced the code to what I thought was a very simple readable state that was still giving me this error that I can not seem to find. I already know it is a stupid error that I am going to smack my head against the wall once it is revealed to me. But for the life of me I can not seem to see the obvious. 3. The lower case return is being used. 4. Thanks for the advice about the tags. I was not sure how to do that.
|
 |
Adam Schaible
Ranch Hand
Joined: Oct 04, 2007
Posts: 101
|
|
Not to be a forum nazi, but this probably belongs in the beginner forum. As Fred has stated, each execution path (meaning each branch of every conditional) must resturn an int. The problem is extremely simple, your convoluted code is making it difficult for you to understand. [ December 13, 2007: Message edited by: Adam Schaible ]
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
[joseph]: 1. The exact error message is "This method must return a result of type int." Well, that certainly makes sense for the code you posted originally, for the reasons which have been explained. But it really doesn't seem possible for the last version of the code you posted (assuming we correct RETURN to return). If you are absolutely sure you are still getting that exact error message with the last version of the code shown, then something is seriously wrong. Perhaps you have more than one file lying around in your filesystem? More than one copy of this method? I suggest checking the filename and line number associated with the error message, very carefully, and verifying exactly which file, method, and line they're talking about. If that doesn't work, then please copy and paste the current method code again, WITHOUT making any extra edits like changing return to RETURN. We need to know what you're really trying to compile. [ December 13, 2007: Message edited by: Jim Yingst ]
|
"I'm not back." - Bill Harding, Twister
|
 |
joseph cooper
Ranch Hand
Joined: Nov 08, 2005
Posts: 44
|
|
|
I have already attempted return statements in all the areas that everyone has listed. I initially listed those areas because I had already attempted these before I asked for advice at JavaRanch. I even tried a return statement in the final clause. I have still not been able to get rid of this error.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9939
|
|
have you confirmed that errorMessage(ee, data) returns an int? "message" to me implies a String. also, the full text of the erorr message should be something like this:
C:\slop>javac -cp . Times.java Times.java:12: missing return statement } ^ 1 error
this tells you the line where the return statement is expected as well as the place. It's not always right, but it does help. [ December 13, 2007: Message edited by: Fred Rosenberger ]
|
 |
Adam Schaible
Ranch Hand
Joined: Oct 04, 2007
Posts: 101
|
|
if you're not going to post the code, but instead write that "I've done all of that but it's still not working" we can't help you. It's fairly simple - one of your execution paths doesn't return a type of int. We've narrowed this down to two possible places. Maybe remove the return errorMessage(blah, blah) and put in return 0; also, after the closing curly brace for the if, put a return 0; - it should compile.
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
[joseph]: I have already attempted return statements in all the areas that everyone has listed. And yet, you haven't posted code that looks like it should compile. The last version seemed close, but you'd altered it. I have the feeling you're making so many changes you're getting careless. Look, the original code you posted had clear, obvious problems which have been explained. Now you say you've tried all these other combinations, but the fact that you posted the original obviously-not-compilable code suggests you don't really understand what the problem was. As such, it's hard for us to just take your word for it that you've tried all these other things. That is, I belive you've tried changing things, but I'm not sure you changed the right things. Again, please show us the exact code you're trying that you belive should fix these problems.
|
 |
joseph cooper
Ranch Hand
Joined: Nov 08, 2005
Posts: 44
|
|
Here is the code with the return statements. I also verified that the initial call of the statement returned an integer.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9939
|
|
I took you code, and boiled it down to the most basic i could: This fails to compile with an "unreachable statement on the "return -2" line. If i comment that one line out out, the code compiles. so, you're either not posting the correct code, or you're not compiling what you think your compiling.
|
 |
joseph cooper
Ranch Hand
Joined: Nov 08, 2005
Posts: 44
|
|
|
I am using WID (Websphere Integration Developer). The method is underlined in red. When I run the pointer over the underlined method the "This method must return a result of type int" error appears. I also get this lovely little red X to the left of the red underlined method. WID is not going to let me comple until the error is addressed and fixed.
|
 |
joseph cooper
Ranch Hand
Joined: Nov 08, 2005
Posts: 44
|
|
|
When I was analyzing the code I figured that you could not reach that particular return statement. That is why I also attempted the else clause. WID still is giving me that ugly little message.
|
 |
Adam Schaible
Ranch Hand
Joined: Oct 04, 2007
Posts: 101
|
|
this is the method I'm talking about: What's the return type? This compiles for me: I had to remove the return -2... it's unreachable as said before, but has nothing to do with the return type.
|
 |
joseph cooper
Ranch Hand
Joined: Nov 08, 2005
Posts: 44
|
|
|
You should also be getting an error stating that the final block should not be complying normally with the return statement in there. This is the major reason I was not wanting to place all the return statements in the initial code because I already knew some of them were obviously wrong. Something else is causing this problem. I will set the code back again to its beginning state and start the refactoring over again to see if I can catch the mistake.
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
It's sounding like WID is not taking a fresh look at what you're doing. I''m not familiar with WID, but perhaps you should look for options such as "rebuild all" or "clean build". Or stop the program and restart it. IF that doesn't work, try commenting out almost all of the method so you're left with Does that, at least, compile? If so, try progressively adding back portions of the commented code, to see where problems emerge, and understand and fix them as they arise. By adding just a little bit at a time, it's easier to understand what happened when new problems emerge. If the above doesn't compile, well at least you know the problem isn't with your method, but somewhere else, probably in WID.
|
 |
 |
|
|
subject: The method must return a result type of int?
|
|
|