Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Problem with return value

Tags:

java

I know it may seem like an easy to solve problem, but I can't find my fault in this piece of code. I'm returning int and Eclipse is telling me that 'This method must return a result of type int'.

public static int getLastId(int table) {

    Connection connection = null;
    String url = "jdbc:postgresql://someServer:port/someDB";

    try {
        //Verbindung herstellen
        connection = DriverManager.getConnection(url, "someUser",
                "somePassword");
        } catch (SQLException e1) {
        //fehlerhafte Verbindung
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        //0 - source ; 1 - destination Table
        if(table == 0){

        Statement stmt = connection.createStatement();
        ResultSet lastId;
        lastId = stmt.executeQuery("SELECT index FROM table0 ORDER BY someIndex DESC LIMIT 1");
        String theLastId0 = "";
         while(lastId.next())
          {

              //System.out.print(lastId.getString("index"));
              theLastId0 = lastId.getString("index");

          }
          lastId.close();
          stmt.close();
          connection.close();
          int letzteId0 = Integer.parseInt(theLastId0);

        return letzteId0;

        }else if(table == 1){

            Statement stmt = connection.createStatement();
            ResultSet lastId;
            lastId = stmt.executeQuery("SELECT index FROM table1 ORDER BY someIndexDESC LIMIT 1");
            String theLastId1 = "";
              while(lastId.next())
              {

                  //System.out.print(lastId.getString("index"));
                  theLastId1 = lastId.getString("index");

              }
              lastId.close();
              stmt.close();
              connection.close();
              int letzteId1 = Integer.parseInt(theLastId1);

            return letzteId1;
            }

    } 
    catch (SQLException e) {

        System.out.println("Connection Failed! Check output console");
        e.printStackTrace();
        return -1;
    }
}
like image 236
1amtoo1337 Avatar asked Sep 01 '26 23:09

1amtoo1337


2 Answers

What happens if table != 0 and table != 1? Then your method doesn't return anything. So either add an else statement to your if or just a regular return, returning a dummy value like -1.

Even if you, the programmer, know that this case will never be executed, the compiler doesn't know that so you gotta make it happy anyways. Plus nasty things can be done using reflection so it's never a good idea to assume the input to your methods are valid.

like image 56
tskuzzy Avatar answered Sep 03 '26 13:09

tskuzzy


This method must Always return an int.

You must add the following else statement

else {
return 0;
}

after the else if because in your version you don't return anything if your if AND your else if evaluates to false.

like image 35
Manuel Selva Avatar answered Sep 03 '26 13:09

Manuel Selva



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!