• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to return class instance?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 177
Hibernate Python Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 177
Hibernate Python Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's still not working.
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
nermin vucinic
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you say "nothing returns" what does that mean?
 
nermin vucinic
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic