I have a method wherein i have to return a 2D String array.
The part of code for that method is as follow:-
public String[][] retrieveData(){
try{
int noOfRows = 0;
pstmt = con.prepareStatement("select * from category");
ResultSet rs = pstmt.executeQuery();
while(rs.next())
noOfRows++;
rs.first();
categoryContent = new String[noOfRows][noOfCols];
for(int i = 0 ; i < noOfRows ; i++){
for(int j = 0 ; j < noOfCols ; j++){
if(j == 0){
Integer categoryNo = new Integer(rs.getInt(1));
categoryContent[i][j] = categoryNo.toString();
}
else{
categoryContent[i][j] = rs.getString(j+1);
}
}
rs.next();
}
return categoryContent ;
}
catch(Exception e){
e.printStackTrace();
}
}
The error which i am getting at compile time is as follows:-
I:\DynamicWebpageCreator\WEB-INF\classes>javac *.java
Category.java:134: missing return statement
public String[][] retrieveData(){
^**
1 error
Please help me soon. I am stuck with it.
All the answers are highly appreciated!
If an exception is thrown, you’ll print the stack trace, but never return anything. That’s what the compiler is complaining about.
In general, “handling” exceptions like this is a really bad idea:
Exception– catch specific exceptionsIn this case I’d suggest you should probably just change your method to declare which exceptions might be thrown, and remove the try/catch block.
If you genuinely want to catch exceptions (specific ones, mind) then you’ll need to work out what you want to return in that case.