This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes missing return value error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "missing return value error" Watch "missing return value error" New topic
Author

missing return value error

Daniel Brackett
Greenhorn

Joined: Dec 09, 2011
Posts: 8

Hi I am trying to return the value SavingsAcc in this coding exercise. I want to do this so that I can put together multiple methods in a single line. What do I need to do to fix it?

Here is the code;


here is the error:



SavingsAcc2.java:29: missing return value
return;
^
SavingsAcc2.java:35: missing return value
return;
^
SavingsAcc2.java:41: missing return value
return;
^
3 errors


I am doing all of this from the command line I plan on using eclipse or DR java soon but I just don't know them yet.


Learning Java on a Ubuntu Linux 10.04 Machine
Brian Burress
Ranch Hand

Joined: Jun 30, 2003
Posts: 122
The method declarations such as "public SavingsAcc2 addInterest(double interest)" indicate a type of "SavingsAcc2" is expected to be returned. The return statement is not returning anything.

Not sure what the intent is. If you do not want anything returned, you can delcare void as the return type "public void addInterest(double interest)". Otherwise, you'll need to return an instance of SavingsAcc2 as it is currently coded.
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
The reason for the error is because your methods:



specify a return type "SavingsAcc2" but don't return a value of that type. In fact, they don't return a value at all of any type. Either change the return type to void, meaning you won't be returning anything, or provide a value of type SavingsAcc2 in your return statement.

Other pointers
- indent your code properly.
- a method will return when it is done, a "return;" is not required.
- I don't know what you mean by "so that I can put together multiple methods in a single line." Can you show an example?


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61071
    
  66

Greg Brannon wrote:- I don't know what you mean by "so that I can put together multiple methods in a single line." Can you show an example?

I believe he's talking about "method chaining".

If the methods all return this, you can do things like:
It's really common in JavaScript, but not so much in Java APIs for some reason.

I frequently wish that the JavaBean standard had specified that setters return this. It'd make code so much terser.

If that's the intent, then all those return statements should be:


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Brian Burress
Ranch Hand

Joined: Jun 30, 2003
Posts: 122
Bear,

Thanks for the comments. I did not understand what what was envisioned either. Returning 'this' would solve the compile errors. While this would be 'legal' in a java sense, I question the value of coding methods to operate this way as you are limiting yourself to be able to do it only with methods which do not return anything.

If there is a need for the user to perform many method calls at once, perhaps a helper method which invokes them all would be a better approach. I digress from the main question a little, sorry!!
Daniel Brackett
Greenhorn

Joined: Dec 09, 2011
Posts: 8

Thank you Bear Bibeault! Success! and yes I was talking about method chaining!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: missing return value error