2

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

3 Answers 3

9

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.

Sign up to request clarification or add additional context in comments.

1 Comment

tskuzzy, your also absolutely right with your statements, but since Manuel Selva gave a (in my opinion) concisely, better & shorter explanation he'll get the Answer point.
4

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.

1 Comment

Thanks very much thats the best answer to this problem. One thing that i wouldn't have thought of even if i reached home from work ^^. Sometimes you need to just ask a question to see how stupid one can be sometimes. Cheers.
1

If table != 0 or 1, then your code does not return anything. Just add return 0; at the very end or whatever the appropriate behavior is, then you should be fine.

For future reference, your methods must return a value in every condition if a return type is specified. If there is a situation where nothing is returned, then the code is faulty. Hope it helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.