Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error states "This method must return a result of type List"

 
Geri Calderon
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am very green and need a bit of help. Here is the code:

public VehSearch() {
}
public List getYear(List carYr){
List yearList = new ArrayList();
Connection connection = null;
Statement stmt = null;
try {
Class.forName(JDBC_DRIVER);
Connection db2Conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
Statement st = db2Conn.createStatement();
ResultSet rs = st.executeQuery("SELECT DISTINCT YEAR FROM VEHICLE");
while(rs.next()) {
String carYear = rs.getString(1);
System.out.println(carYear);
}
rs.close();
st.close();
db2Conn.close();
}

I am not sure where to put the return or what to put in it for that matter. Any direction would be great!
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Golf Queen,"

Welcome to JavaRanch! Please check your private messages by clicking on My Private Messages. Thanks!
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your getYear method declares that it returns a List, so all normal paths of execution within the method body must end with a statement that returns a List.

It looks like your method is just printing years to the console. If that's all you want to do, then there's no need to return anything, and your method can declare a return type of "void" instead of "List."

But if you do want the method to return a List, then you should be adding these years to your List (yearList) as you get them from the ResultSet, and then return yearList when you're done.
 
Silpa Sunkavalli
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public VehSearch() {
}
public List getYear(List carYr){
List yearList = new ArrayList();
Connection connection = null;
Statement stmt = null;
try {
Class.forName(JDBC_DRIVER);
Connection db2Conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
Statement st = db2Conn.createStatement();
ResultSet rs = st.executeQuery("SELECT DISTINCT YEAR FROM VEHICLE");
while(rs.next()) {
String carYear = rs.getString(1);
//Add the carYear element to the list variable
yearList.add(carYear);
System.out.println(carYear);
}
rs.close();
st.close();
db2Conn.close();
//Here you have to return the list
return yearList;


}
 
Geri Calderon
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both for your input. I made the changes and am still getting the error:

I must be missing something in the translation...

[edit]Add code tags and change indentation. CR[/edit]
[ September 09, 2008: Message edited by: Campbell Ritchie ]
 
Norm Radder
Rancher
Posts: 1219
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the full text of the error message?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49755
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please always use the code button when posting code, and make sure the indentation is right. I have edited your last post and you can see how much easier it is to read. You haven't posted the entire method; is there anything after what you have posted other than a catch block?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49755
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what are you doing with the carYr parameter? It doesn't appear to be used in that method, in which case you ought to lose the parameter.
 
Geri Calderon
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the enter code (with company info blocked out)


[ September 09, 2008: Message edited by: Geri Calderon ]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Geri Calderon:
Here's the catch

I'm trying to get the data from the data base and the yearList goes into a drop down box.



So if an ClassNotFoundException or SQLException exception is thrown, what gets returned from the method ?
[ September 09, 2008: Message edited by: Joanne Neal ]
 
Geri Calderon
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for everyone's help. found the problem. after the last catch:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic