Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Missing Return Statement

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please I will really appreciate if anyone could point to me what I am doing wrong here. I have been on this for 72 hours now without much success and have reached the point where I need someone to give a hint
on my mistake.

I keep getting this error of "Missing Return Statement" even though I have a return statement. my code snippet is below:


// Method to return an Array Object
//======================================
public Object[][] queryData() {

Connection ThisSqlConnection = mysqlConnection( );
try {
Statement statement = ThisSqlConnection.createStatement();
String SqlQuery = ("select * from Test");
results = statement.executeQuery(SqlQuery);

while (results.next()){
int id = results.getInt("e_id" );
String name = results.getString("name");
int age = results.getInt("age" );
String state = results.getString("state");
String location = results.getString("location");
String date = results.getString("date");

Object[][] dataValues = {{id, name, age, state, location, date}};

return dataValues;

}
}catch ( SQLException ex ){

System.out.println("SQLException:" + ex.getMessage());


}
 
author
Posts: 3281
8
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jonas okwara:
Please I will really appreciate if anyone could point to me what I am doing wrong here. I have been on this for 72 hours now without much success and have reached the point where I need someone to give a hint
on my mistake.

I keep getting this error of "Missing Return Statement" even though I have a return statement. my code snippet is below:


// Method to return an Array Object
//======================================
public Object[][] queryData() {

Connection ThisSqlConnection = mysqlConnection( );
try {
Statement statement = ThisSqlConnection.createStatement();
String SqlQuery = ("select * from Test");
results = statement.executeQuery(SqlQuery);

while (results.next()){
int id = results.getInt("e_id" );
String name = results.getString("name");
int age = results.getInt("age" );
String state = results.getString("state");
String location = results.getString("location");
String date = results.getString("date");

Object[][] dataValues = {{id, name, age, state, location, date}};

return dataValues;

}
}catch ( SQLException ex ){

System.out.println("SQLException:" + ex.getMessage());


}



*Hint* If your code has an SQL Exception, will it always execute a return statement?
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot put you return code only inside the 'try' block.
Either you put it at the end of the member function or both inside try and catch block.
 
jonas okwara
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks mario for your prompt response. When I put the return statement in the catch block or at the end of the member function, the returned
Object ( dataValues ) becomes hidden and I get the error message It cannot see "dataValues"
 
Martijn Verburg
author
Posts: 3281
8
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jonas okwara:
Thanks mario for your prompt response. When I put the return statement in the catch block or at the end of the member function, the returned
Object ( dataValues ) becomes hidden and I get the error message It cannot see "dataValues"



That's because you've declared the dataValues variable inside the Try block, the catch block does not know about it. You need to place that variable declaration in a 'higer' scope, e.g. Above the try block.

Cheers,
Martijn
 
Ranch Hand
Posts: 1143
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mario Minutolo:
You cannot put you return code only inside the 'try' block.
Either you put it at the end of the member function or both inside try and catch block.



Yes, you can, but only if the entire method body is within a "try-catch" block, which is not the case for the code that Jonas posted.

But don't take my word for it, try it out yourself.

Good Luck,
Avi.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic