Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Missing Return Statement

 
jonas okwara
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • 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());


}
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • 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?
 
Mario Minutolo
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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
  • 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
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • 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
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic