2017-10-06 96 views
1

此ProductDAO类返回用户的产品列表,但Netbeans中的编译器显示“缺少返回语句”。任何进展?缺少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){ 
     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(); }   
     }} } 
+0

如果在上次try-catch中发生异常,在此块或catch/finally子句中没有'return'语句。您需要为每个可能的执行都有一个'return'语句。 – AxelH

+0

[在非void方法编译时缺少return语句](https:// stackoverflow。com/questions/16789832/missing-return-statement-in-a-non-void-method-compiles) – Sturzl

+1

如果你学习java,你需要学习https://docs.oracle.com/javase/tutorial/java/ javaOO/returnvalue.html在发布这种类型的问题之前堆栈溢出https://stackoverflow.com/help/how-to-ask,其中Netbeans IDE已经知道你关于这个编译时间**缺少返回语句** – 2017-10-06 12:28:46

回答

1

如果方法不是void方法(像这样),所有的方法可能的分支必须返回的东西。在这种情况下,如果在第二个try块中抛出异常,则不会返回任何内容。

您可以将return cl语句移至方法的末尾而不是try块的末尾。

+0

是,或返回null,或抛出异常 –

+0

因为我会在这里抛出异常。特别是因为列表在开始时被初始化,所以在不知道任何异常的情况下(除了在日志中),您将拥有一个空列表。 (或'null',但我最近尝试使用异常而不是null) – AxelH

0

无论发生什么事情(换句话说,在每个执行路径中),都应该返回。

在每个没有返回的分支中添加一个空列表的返回语句(或任何你喜欢的默认值)。

也就是说,在这里:

catch(SQLException e){ 
    e.printStackTrace(); 
    return new ArrayList<Product>();  // <-- 
} 
-1

你认为:

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; 

} 

    } 
0

缺少return语句的Netbeans ProductDao的

你try块返回CL但缺少catch块return语句()和方法doSelectAvailableProducts结束,通过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;} 
0

如果通过你的代码的一些路径到达最终你得到此警告该方法没有击中return语句,并且该方法的返回类型不是无效的。

您可以:添加一个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? 
}