Search code examples
try-catchequalsif-statementfinally

Else-part of code being executed, even if string.equals(otherstring) is true


The problem with this code seems to be that the 'else' part of the if-statement is executed, even if the variables match (so 'if' is true). Any advice, please?

Thanks!

    public void CheckInstalledDBVersion() throws NullPointerException, IOException {

    try {

                  //TRY TO OPEN DATABASE AND READ VERSION
                  //WRITE VERSION TO InstalledDBversion

    } catch(RuntimeException e) {

                  //IF TABLE COULD NOT BE QUERIED
                  //SET InstalledDBversion to Bogus value

        InstalledDBversion = "00";
        Log.d("RTE", ".. but we've catched it!");

    } finally {
        if (InstalledDBversion.equals(PackedDBversion)){

            // Installed DBVersion == Packed DBVersion .. nothing happens

        }
        else

                showDialog(DBCHECKFAILDIALOG);
            initialiseDatabase = false;
            copyDB();
        }
    }


So, when I execute, copyDB(); gets called even if InstalledDBversion.equals(PackedDBversion) == true


Solution

  • else
       showDialog(DBCHECKFAILDIALOG);
    
    initialiseDatabase = false;
    copyDB();
    

    Fixed indentation for you. copyDB is outside of the if/then/else block. Use an IDE with code formatting.