Solved Missing Return Statement?

Discussion in 'Spigot Plugin Development' started by MangoCodes, May 26, 2016.

  1. Code (Text):

    public int getPlayerTokens(Player p) {
        Connection con = getMySQLConnection();
        if(con != null) {
            try{
                Statement st = con.createStatement();
                ResultSet rs = st.executeQuery("SELECT * FROM Tokens WHERE UUID='" + p.getPlayer().getUniqueId().toString().toLowerCase() + "'");
                if(rs.next()) {
                    rs.getInt("TOKENS");
                }else{
                    return 0;
                }
            }catch (SQLException e) {
                e.printStackTrace();
            }finally {
                if(con != null) {
                    try {
                        con.close();
                    }catch (SQLException e1) {
                        e1.printStackTrace();
                    }
                   
                }
            }
        }
    } --- > Missing return statement???
     
    GIF: https://gyazo.com/639a61662f7693ca405f4e64b6bb361c
     
  2. If the connection does equal null then nothing will be returned...
     
  3. Code (Text):
    public int getPlayerTokens(Player p) {
        Connection con = getMySQLConnection();
        if(con != null) {
            try{
                Statement st = con.createStatement();
                ResultSet rs = st.executeQuery("SELECT * FROM Tokens WHERE UUID='" + p.getPlayer().getUniqueId().toString().toLowerCase() + "'");
                if(rs.next()) {
                    return rs.getInt("TOKENS");
                }else{
                    return 0;
                }
            }catch (SQLException e) {
                e.printStackTrace();
            }finally {
                if(con != null) {
                    try {
                        con.close();

                    }catch (SQLException e1) {
                        e1.printStackTrace();
                    }
                   
                }
            }
        }
        return 0;
    }
    You were missing a return statement incase the connection failed, and you also didn't return rs.getInt("TOKENS");
     
  4. A missing return statement means you.. missed a return statement. It does not exist. It should. A method cannot just simply return nothing if it has a return type.
     
    • Agree Agree x 1
  5. You created a method that returns a integer, you must return a int.
     
  6. eh ? Indeed you'll miss return statement

    the method called int need to return with value int

    Code (Java):
    public int i () {
    return 0;
    }
    similar to

    Code (Java):
    int i = 0;
     
    • Like Like x 1
  7. To expand on this, you'll get an error if you did this:
    Code (Java):
    public int i() {

    }
     
    • Informative Informative x 1