File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes How to return class instance? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to return class instance?" Watch "How to return class instance?" New topic
Author

How to return class instance?

nermin vucinic
Greenhorn

Joined: Aug 23, 2011
Posts: 9
I have the next code:



Now, what I need is to make this method(getFromId) return this newly created class instance, with its fields values.
Any suggestons?
Manuel Petermann
Ranch Hand

Joined: Jul 19, 2011
Posts: 175

I would suggest you have a look at dao classes.
If you want to do it your way, which I think is bad design, you just need to return the products or if none are recieved you return null.


Please correct my English.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

First I would change the method so it's declared to return that object. So, not

but


(And no, that isn't a typo, I would call the class "Product" and not "Products" because it contains the data for one product.)

Then somewhere in that code I would insert a line to return a reference to a Product object. I leave it to you to work out where you should insert that line.
nermin vucinic
Greenhorn

Joined: Aug 23, 2011
Posts: 9
You mean like this:

I tried it, but it's not working(error message saying MISSING RETURN STATEMENT in this class, but also in main class,from which I call this method).
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

When you say "not working", it's helpful to give more details.

Remember, though, that if the method is declared to return something, then it has to return something in all possible paths through the method. What are you returning if an SQLException is thrown? Or if the result set is empty?
Manuel Petermann
Ranch Hand

Joined: Jul 19, 2011
Posts: 175

It was not working because you had no return type.
If you follow Paul's answer you should get it to work as expected.
nermin vucinic
Greenhorn

Joined: Aug 23, 2011
Posts: 9
It's still not working.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Matthew Brown wrote:When you say "not working", it's helpful to give more details.
nermin vucinic
Greenhorn

Joined: Aug 23, 2011
Posts: 9
Paul Clapham wrote:
Matthew Brown wrote:When you say "not working", it's helpful to give more details.

There was this error message:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - missing return statement
at sales.Product.getFromId(Product.java:158)
at sales.Sales.main(Sales.java:18)
Java Result: 1


so I put
return null
statement at the the end of class.
Now there is no error messages, but this method when called from main class(Sales), returns nothing.
Here is new code:
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Don't ever put a return statement inside a finally block. That overrides any returns you might have done in the try statement. (Yes, you do have a return statement inside a finally block, it's just that the irregular indentation hides that fact.)

It's all right to put the return statement after the finally block.
nermin vucinic
Greenhorn

Joined: Aug 23, 2011
Posts: 9
Paul Clapham wrote:Don't ever put a return statement inside a finally block. That overrides any returns you might have done in the try statement. (Yes, you do have a return statement inside a finally block, it's just that the irregular indentation hides that fact.)

It's all right to put the return statement after the finally block.

I changed it, but result is the same(no errors, but nothing returns).
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

When you say "nothing returns" what does that mean?
nermin vucinic
Greenhorn

Joined: Aug 23, 2011
Posts: 9
Paul Clapham wrote:When you say "nothing returns" what does that mean?

It means when I run this method (getFromId) from main class (Sales), it should return new class instance(Product p), with its field values, but it returns nothing.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

nermin vucinic wrote:It means when I run this method (getFromId) from main class (Sales), it should return new class instance(Product p), with its field values, but it returns nothing.

I assume you mean null is returned, which isn't quite the same thing. Anyway, assuming the return null is now in the right place, the possible explanations for that that I can think of are:

- An exception is thrown somewhere (though you should get something written to System.out in those cases)
- The query isn't returning a match for the given ID

You can put some debugging lines in to check if it's the latter - does rs.next() return false? If so, I think the next step would be to check that the ID value being passed in is what you think it is, and that the database contains what you think it does.
nermin vucinic
Greenhorn

Joined: Aug 23, 2011
Posts: 9
Matthew Brown wrote:
nermin vucinic wrote:It means when I run this method (getFromId) from main class (Sales), it should return new class instance(Product p), with its field values, but it returns nothing.

I assume you mean null is returned, which isn't quite the same thing. Anyway, assuming the return null is now in the right place, the possible explanations for that that I can think of are:

- An exception is thrown somewhere (though you should get something written to System.out in those cases)
- The query isn't returning a match for the given ID

You can put some debugging lines in to check if it's the latter - does rs.next() return false? If so, I think the next step would be to check that the ID value being passed in is what you think it is, and that the database contains what you think it does.

I test it, but it's not the issue.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

Sorry, but none of your posts are really giving us enough information to help us help you.

By now, by either running through a debugger or adding debugging code (print statements, etc), you should have been able to:

a) Confirm that the method has been called in the first place
b) Confirmed exactly where the method is returning from

That might give us a clue where to look next.

It might be worth posting the current version of the method, and tell us exactly what path through the code is being taken.
nermin vucinic
Greenhorn

Joined: Aug 23, 2011
Posts: 9
Matthew Brown wrote:Sorry, but none of your posts are really giving us enough information to help us help you.

By now, by either running through a debugger or adding debugging code (print statements, etc), you should have been able to:

a) Confirm that the method has been called in the first place
b) Confirmed exactly where the method is returning from

That might give us a clue where to look next.

It might be worth posting the current version of the method, and tell us exactly what path through the code is being taken.




When I call method from main class(Sales), it returns nothing,null (also no errors reported), instead it should return new class instance and its field values.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

You're still returning null in the finally block. That's the error Paul told you about earlier, and you said you'd changed. That means the method will always return null no matter what happens.
 
GeeCON Prague 2014
 
subject: How to return class instance?