[英]Missing return statement compiler warning
该ProductDAO类返回给用户的产品列表,但是Netbeans中的编译器显示“ Missing return statement”。 有进步吗?
public List<Product> doSelectAvailableProducts( String userid){
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
List<Product> cl = new ArrayList<Product>();
try{
con = datasource.getConnection();
}
catch( SQLException e){
e.printStackTrace();
}
try{
stmt = con.createStatement();
String sql = "select * from Product where id not in " +"(select cid from registration where uid ='" + userid + "')";
rs = stmt.executeQuery(sql);
while( rs.next() ){
Product product = new Product();
product.setId(rs.getInt("id"));
product.setName(rs.getString("name"));
product.setDescription( rs.getString("description"));
product.setPrice( Double.parseDouble(rs.getString("price"))); cl.add(product); }
return cl;
}
catch( SQLException e ){
e.printStackTrace();
}
finally {
if(stmt != null) {
try { stmt.close();}
catch (Exception e) { e.printStackTrace(); }
}
if(con != null) {
try { con.close();}
catch (Exception e) { e.printStackTrace(); }
}} }
如果该方法不是void
方法(像这样),则该方法的所有可能分支都必须返回某些内容。 在这种情况下,如果在第二个try
块中引发了异常,则不会返回任何内容。
您可以将return cl
语句移到方法的末尾而不是try
块的末尾。
无论发生什么(换句话说,在每个执行路径中),您都应该返回。
在每个不返回的分支中添加一个空列表的return语句(或您喜欢的默认值)。
也就是说,在这里:
catch( SQLException e ){
e.printStackTrace();
return new ArrayList<Product>(); // <--
}
缺少退货声明Netbeans ProductDAO
您的try块返回cl,但是catch块()和方法doSelectAvailableProducts的结尾缺少return语句,这是JVM的时间通知,
如果您的方法返回某些内容,则它必须处理所有方案以返回某些内容。
List<Product> doSelectAvailableProducts result=object.doSelectAvailableProducts(aStringId);
if(result!=null){
// Do you next work flow here...
}
注意:返回引用,形成另一面,您可能会得到NullPointerException ,如果不检查的话
public List<Product> doSelectAvailableProducts( String userid){
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
List<Product> cl = new ArrayList<Product>();
try{
con = datasource.getConnection();
}
catch( SQLException e){
e.printStackTrace();
return cl;
}
try{
stmt = con.createStatement();
String sql = "select * from Product where id not in " +"(select cid from registration where uid ='" + userid + "')";
rs = stmt.executeQuery(sql);
while( rs.next() ){
Product product = new Product();
product.setId(rs.getInt("id"));
product.setName(rs.getString("name"));
product.setDescription( rs.getString("description"));
product.setPrice( Double.parseDouble(rs.getString("price"))); cl.add(product); }
return cl;
}
catch( SQLException e ){
e.printStackTrace();
return cl;
}
finally {
if(stmt != null) {
try { stmt.close();}
catch (Exception e) { e.printStackTrace(); }
}
if(con != null) {
try { con.close();}
catch (Exception e) { e.printStackTrace(); }
}} return cl;}
如果代码中的某些路径未到达return语句而到达方法的末尾,并且该方法的返回类型不是void,则会收到此警告。
您可以:添加return语句,引发所捕获的异常之一,或将方法更改为不返回任何内容。
简要说明一下:代码格式化将帮助您更轻松地了解这种情况,并帮助那些了解您的代码的人。 我已经格式化了以下代码,并在注释中显示了一些用于修复编译器错误的选项。
public List<Product> doSelectAvailableProducts( String userid){
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
List<Product> cl = new ArrayList<Product>();
try{
con = datasource.getConnection();
}
catch( SQLException e){
//throw this exception or add a return here?
e.printStackTrace();
}
try{
stmt = con.createStatement();
String sql = "select * from Product where id not in " +"(select cid from registration where uid ='" + userid + "')";
//Exception might be thrown here, so the return statment below isn't reached
//then all the code in the catch and finally run, but nothing is returned
rs = stmt.executeQuery(sql);
while( rs.next() ){
Product product = new Product();
product.setId(rs.getInt("id"));
product.setName(rs.getString("name"));
product.setDescription( rs.getString("description"));
product.setPrice( Double.parseDouble(rs.getString("price"))); cl.add(product); }
// Last return statement, never reached
return cl;
}
catch( SQLException e ){
//throw this exception or add a return here?
e.printStackTrace();
}
finally {
if(stmt != null) {
try {
stmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
if(con != null) {
try {
con.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
//add a return here?
}
//add a return here?
}
你认为:
finally {
if(stmt != null) {
try { stmt.close();}
catch (Exception e) { e.printStackTrace(); }
}
if(con != null) {
try { con.close();}
catch (Exception e) { e.printStackTrace(); }
}
}
return null;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.