【问题标题】:Missing return statement compiler warning缺少返回语句编译器警告
【发布时间】:2018-03-18 05:24:07
【问题描述】:

此 ProductDAO 类返回用户的产品列表,但 Netbeans 中的编译器显示“缺少返回语句”。有预支吗?

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(); }        
      }}  }

【问题讨论】:

标签: java compiler-errors return


【解决方案1】:

缺少返回语句 Netbeans ProductDAO

你的 try 块返回 cl 但是在 catch 块 () 和方法结束时缺少 return 语句 doSelectAvailableProducts ,这是 JVM 的编译时间通知,

如果你的方法返回一些东西,那么它必须处理所有场景 返回一些东西。

List<Product> doSelectAvailableProducts result=object.doSelectAvailableProducts(aStringId);
if(result!=null){
// Do you next work flow here...
}

注意:return reference,Form another side,你可能会得到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;}

【讨论】:

    【解决方案2】:

    如果您的代码中的某些路径到达方法的末尾而没有遇到 return 语句,并且该方法的返回类型不是 void,您会收到此警告。

    您可以:添加 return 语句,抛出您正在捕获的异常之一,或更改方法以不返回任何内容。

    快速附注:代码格式化将帮助您更轻松地了解此类内容,并帮助那些在您理解您的代码之后的人。我已经格式化了下面的代码并放入了 cmets,显示了一些修复编译器错误的选项。

    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?
    }
    

    【讨论】:

      【解决方案3】:

      你认为:

      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;
      
      }
      
        }
      

      【讨论】:

        【解决方案4】:

        如果该方法不是void 方法(如这个),则该方法的所有可能分支都必须返回一些内容。在这种情况下,如果在第二个 try 块中引发异常,则不会返回任何内容。

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

        【讨论】:

        • 是,要么返回null,要么抛出异常
        • 我会在这里抛出异常。特别是因为列表是在开始时初始化的,所以您将有一个空列表而不知道任何异常(日志除外)。 (或null,但我最近尝试使用异常而不是null)
        【解决方案5】:

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

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

        也就是说,这里:

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

        【讨论】:

          猜你喜欢
          • 2015-09-19
          • 2015-05-16
          • 2015-06-01
          • 1970-01-01
          • 2014-08-20
          • 2013-10-27
          • 1970-01-01
          • 2017-05-26
          • 1970-01-01
          相关资源
          最近更新 更多