簡體   English   中英

缺少返回語句編譯器警告

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM