wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes missing return statement error message when compiling class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "missing return statement error message when compiling class" Watch "missing return statement error message when compiling class" New topic
Author

missing return statement error message when compiling class

Stuart Lord
Greenhorn

Joined: Oct 27, 2006
Posts: 27
Whilst attempting an exercise in the SCJP study guide - (Exercise 5-3, pages 352 - 353), I get the following error message on compilation:

Propagate.java:15: missing return statement
}
^
1 error

Below is the relevant part of the class that I've written.



Clearly reverseStr IS being returned - immediately after the for loop - so whats going on??
[ March 08, 2007: Message edited by: Stuart Lord ]
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
In my opinion, the problem is with the location of the return statement. Since it is within the if() loop, there is a possibility of the routine running without returning anything, and this is forbidden.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
I would make the following changes:

Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
With other words: what should the method return if in.length() <= 0?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Stuart Lord
Greenhorn

Joined: Oct 27, 2006
Posts: 27
Many thanks for this Pete - I tried your solution and got rid of the error message. Clearly the problem lay with the scope of reverseStr - i.e.it had to be accessible outside of the if() construct.

To answer Ilja's question - the object of the exercise is to write a class that demonstrates exception handling and so if no argument was entered the length of in would be 0. This throws the ArrayIndexOutOfBoundsException exception, and so the catch should be trapping that first.

I've amended the class so as to display the value of reverseStr outside of the try/catch/finally block, but am getting the following compilation error:

Propagate.java:34: reverse(java.lang.String) in Propagate cannot be applied to (
)
System.out.println("The new string is " + reverse());
^
1 error

I'll include the entire class below:

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Again, I'd change this:

to this:


Next, you have to either change your reverse method to throw ArrayIndexOutOfBoundsException or change your catch statement to catch all exceptions.

Finally, you can't call reverse() without a parameter as you do here:


Consider using a string variable.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

you have to either change your reverse method to throw ArrayIndexOutOfBoundsException or change your catch statement to catch all exceptions.

Why throwing ArrayIndexOutOfBoundsException ? This is a RuntimeException. I don't think you should deal with it.


[My Blog]
All roads lead to JavaRanch
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Originally posted by Stuart Lord:
Many thanks for this Pete - I tried your solution and got rid of the error message. Clearly the problem lay with the scope of reverseStr - i.e.it had to be accessible outside of the if() construct.

To answer Ilja's question - the object of the exercise is to write a class that demonstrates exception handling and so if no argument was entered the length of in would be 0. This throws the ArrayIndexOutOfBoundsException exception, and so the catch should be trapping that first.

Exactly where is that exception being thrown?
Stuart Lord
Greenhorn

Joined: Oct 27, 2006
Posts: 27
Just a few points to clear up here:

First: You can see whats in reverseStr by adding the line

immediately after the if() construct in the reverse method and yes, the end condition should be

Secondly: To simplify matters I changed the catch statement to read and deleted the and then ran the code with no parameter - i.e. java Propagate the catch trapped the exception with the report:

The string's empty!!
0
java.lang.ArrayIndexOutOfBoundsException: 0
at Propagate.main(Propagate.java:22)
End of main method..


So whats being trapped is the ArrayIndexOutOfBoundsException and the catch should be changed to trap this first. The exception is being thrown in the method and is 'caught' by the catch but as a 'general' exception.
Stuart Lord
Greenhorn

Joined: Oct 27, 2006
Posts: 27
Many thanks to all who have contributed with their help and suggestions - just 2 final things - the value of reverseStr is displayed follows:


(as per Pete's suggestion)

and secondly regarding Satou's remark - in this class this exception is trapped in a catch statement - but not thrown. I would normally expect to check the value of the argument string as the first statement within main, but then the whole purpose of this exercise is to demonstrate exception handling..!
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Originally posted by Stuart Lord:
... got rid of the error message. ...


I work almost exclusively by the cut/copy/paste/blunder/groan approach. This is called cut and try in traditional engineering, I am sure the Object Oriented camp calls this horrible and unspeakable.

Consequent to my approach, I have learned to to tediously follow the code execution path from the very first point one can trace it back to and walk, step-by-step, through every statement and all operators in each statement.

Just because you got rid of the error message does not mean that you fixed the problem. There seems to be plenty of discussion, but obvious to me is the necessity of checking before trying to assign it or do something with

Failure to do this passes the Exception handling beyond the scope of your code, ouch.
[ April 29, 2007: Message edited by: Nicholas Jordan ]

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38513
    
  23
I am afraid you are getting all confused about which Exception you get.

If you don't pass a Command Line argument, you will get an ArrayIndexOutOfBoundsException, thrown in the main method when you try to handle args[0].

You can't get an Exception from the reverseString method, however hard you try, because you have written the "for" loop correctly. If you pass in a zero-length String, you get a zero-length String back, with all the letters it hasn't got in reverse order! I tried it:and I got this output, where "" means sending an empty String as a command line argument.

[Campbell@dhcppc0 disk-1]$ java trivia.StringReverserTest
Exception occurred: 0
[Campbell@dhcppc0 disk-1]$ java trivia.StringReverserTest ""
"" reversed is ""

[Campbell@dhcppc0 disk-1]$ java trivia.StringReverserTest Campbell
"Campbell" reversed is "llebpmaC"

[Campbell@dhcppc0 disk-1]$ java trivia.StringReverserTest Campbell Ritchie
"Campbell" reversed is "llebpmaC"

[Campbell@dhcppc0 disk-1]$ java trivia.StringReverserTest "Campbell Ritchie"
"Campbell Ritchie" reversed is "eihctiR llebpmaC"
Note the difference between Campbell Ritchie and "Campbell Ritchie".

CR
[ April 30, 2007: Message edited by: Campbell Ritchie ]
 
jQuery in Action, 2nd edition
 
subject: missing return statement error message when compiling class