1

This ProductDAO class returns with a list of products for the user, but the compiler in Netbeans shows that "Missing return statement." Any advance?

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(); }        
      }}  }
Avery Sturzl
  • 82
  • 1
  • 13
HPMD
  • 21
  • 4
  • If an exception occurs in your last try-catch, there is no `return` statement after this block or in the catch/finally clause. You need to have one `return` statement for EVERY possible execution. – AxelH Oct 06 '17 at 11:56
  • Possible duplicate of [Missing return statement in a non-void method compiles](https://stackoverflow.com/questions/16789832/missing-return-statement-in-a-non-void-method-compiles) – Avery Sturzl Oct 06 '17 at 12:20
  • 1
    If you learning java,You need learn from https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html before post this type of question on stack-overflow https://stackoverflow.com/help/how-to-ask, Where Netbeans IDE already aware you about this compile time **Missing return statement** –  Oct 06 '17 at 12:28

5 Answers5

1

If the method isn't a void method (like this one), all the possible branches of the method must return something. In this case, you aren't returning anything if an exception is thrown in the second try block.

You could just move the return cl statement to the end of the method instead of the end of the try block.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Yes, or return null, or throw an exception – Maurice Perry Oct 06 '17 at 12:02
  • I would throw the exception here since. Especially because the list is initialize at the beginning, you would have a empty list without knowing about any exception (except in the logs). (or `null` but I am trying to use exception instead of null lately) – AxelH Oct 06 '17 at 12:05
0

You should return no matter what happens (in other words, in every execution path).

Add a return statement of an empty list (or whatever default you like) in every branch that doesn't return.

That is, here:

catch( SQLException e ){ 
    e.printStackTrace(); 
    return new ArrayList<Product>();     // <--
}
Neo
  • 3,534
  • 2
  • 20
  • 32
0

Missing return statement Netbeans ProductDAO

Your try block return cl but Missing return statement in catch block () and end of method doSelectAvailableProducts , It's complied time notification by JVM,

If your method return something then It must handle all scenario to return something.

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

Note: return reference, Form another side, You may get NullPointerException, If not check It

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

You get this warning if some path through your code gets to the end of the method without hitting a return statement, and the return type of the method is not void.

You can: Add a return statement, throw one of the exceptions you are catching, or change the method to not return anything.

A quick side note: Code formatting will help you see this kind of thing more easily, and help those who come after you understand your code. I've formatted the code below and put in comments showing some of your options for fixing the compiler error.

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?
}
Avery Sturzl
  • 82
  • 1
  • 13
-1

Do you think:

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;

}

  }
HPMD
  • 21
  • 4