-3

I dont know why aI can't return the true or false. It prints "missing return statement." I have looked for answers in the web, but none of them solves my problem.

public boolean Verification(String SQL) throws IOException{
    try{
        Statement statementCount=connection.createStatement();
        ResultSet results = statementCount.executeQuery(SQL);

        String Cont=results.getString("ContadorFecha");
        int cont=Integer.parseInt(Cont);
        if (cont>=10){
            return true;
        } else {
            return false;
        }

    }catch(SQLException ex) {
            ex.printStackTrace();
            System.out.println("Exception in register: " + ex);
        }

}
AJAA
  • 7
  • 1
    You don't return anything in the `catch` block. So there is a missing return statement, as the error says. – bcsb1001 Apr 07 '16 at 10:14
  • 1
    *"but none of them solves my problem"* Sorry, but that's just completely untrue. There are *thousands* of versions of this question, with clear answers, many of them right here on SO. The briefest search gives us [this one](http://stackoverflow.com/questions/33536284/java-missing-return-statement-after-try-catch) with `try/catch`, [this one](http://stackoverflow.com/questions/18788204/missing-return-statement-error-in-java) with `if/else`, [this one](http://stackoverflow.com/questions/35806909/missing-a-return-statement-somewhere) with just `if`... – T.J. Crowder Apr 07 '16 at 10:17

4 Answers4

0

You need to have a return statement for all the scenario's possible.

In this example, it seems to be, because of this:

if (cont>=10){
            return true;
        } else {
            return false;
        }

which could actually be replaced by:

return cont >= 10;

but: what if there's an Exception thrown before that?

The try block is finished, and this is handled (if it's this type of Exception, anyway)

catch(SQLException ex) {
            ex.printStackTrace();
            System.out.println("Exception in register: " + ex);
        }

add a

return false;

after your catch block, and all your cases will be covered. The JVM will know for each scenario what to return, and it will work.

Stultuske
  • 9,296
  • 1
  • 25
  • 37
0

Its missing a return statement because of your catch block.

You either need to return something in there as well, or throw an Exception.

OH GOD SPIDERS
  • 3,091
  • 2
  • 13
  • 16
  • Only half the answer: what if another (not caught) type of Exception is thrown? – Stultuske Apr 07 '16 at 10:16
  • 1
    > what if another (not caught) type of Exception is thrown If a RuntimeException is thrown you don't need a return in that case (obviously) – OH GOD SPIDERS Apr 07 '16 at 10:18
  • that means the catch block will not run, so if you only have a return statement, or throws Exception in that catch block, you still need to cover the possibility of another Exception being thrown. – Stultuske Apr 07 '16 at 10:20
  • I really don't get what you are trying to tell me. – OH GOD SPIDERS Apr 07 '16 at 10:23
  • try{ // code that throws NullPointerException return value; }catch(SQLException e){ // CATCHES ONLY SQLEXCEPTION, not the NPE return otherValue; } // still no return value for the case caused by the NPE – Stultuske Apr 07 '16 at 10:25
  • @Stultuske: Yes...BUT so what? If a Runtime Exception is thrown, or if an Exception is thrown that leaves the method, then you don't need a return.... Example: public String test() throws IOException { if(new Random().nextBoolean()) { throw new IOException(); } throw new RuntimeException(); } – OH GOD SPIDERS Apr 07 '16 at 10:27
  • 1
    @Stultuske: Adding a `return` to the `catch` block is all that's required. If something that isn't an `SQLException` occurs, the method *throws*, it doesn't *return*. – T.J. Crowder Apr 07 '16 at 10:28
  • 1
    @T.J.Crowder nyah ... long day – Stultuske Apr 07 '16 at 10:30
  • 1
    @911DidBush just ignore my remarks – Stultuske Apr 07 '16 at 10:31
0

Simply change it to this:

public boolean Verification(String SQL) throws IOException {
    try {
        Statement statementCount = connection.createStatement();
        ResultSet results = statementCount.executeQuery(SQL);

        String Cont = results.getString("ContadorFecha");
        int cont = Integer.parseInt(Cont);
        if (cont >= 10) {
            return true;
        }

    } catch (SQLException ex) {
        ex.printStackTrace();
        System.out.println("Exception in register: " + ex);
    }
    return false;
}
dambros
  • 4,252
  • 1
  • 23
  • 39
0
public boolean Verification(String SQL) throws IOException{
      Boolean flag=false;
        try{
            Statement statementCount=connection.createStatement();
            ResultSet results = statementCount.executeQuery(SQL);

            String Cont=results.getString("ContadorFecha");
            int cont=Integer.parseInt(Cont);
            if (cont>=10){
                flag=true;
            }

        }catch(SQLException ex) {
                ex.printStackTrace();
                System.out.println("Exception in register: " + ex);
            }
        return flag;

    }
P S M
  • 1,121
  • 12
  • 29