File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes missing return value error Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 4

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: 118
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
Rancher

Joined: Oct 24, 2010
Posts: 380
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 opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50691

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:


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Brian Burress
Ranch Hand

Joined: Jun 30, 2003
Posts: 118
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: 4

Thank you Bear Bibeault! Success! and yes I was talking about method chaining!
 
 
subject: missing return value error
 
Threads others viewed
Program like ATM where person enters amount in dollars and cents but program uses int for monies
Constructor issues with creating a new checking account object.
need help urgent please
Having trouble understanding an error code i keep getting. Please help!
Just Can't figure it out and it's due today
developer file tools