0

My while loop in Java doesn't loop even though the boolean is still false (I think?).

If I remove all the "mysqlStatus = true" it still doesn't want to loop. I don't really understand how that is possible as I never switch the boolean to "true".

It's probably something stupid though so thanks in advance for the help :).

The loop:

    Connection conn;
    Boolean mysqlStatus = false;

    while(mysqlStatus.equals(false)) {

        try {
            Class.forName(driver).newInstance();
            conTest = DriverManager.getConnection(host + dbName, username, password);
            mysqlStatus = true;
        } catch (SQLException e) {
            // server offline

            Alert alert = new Alert(AlertType.CONFIRMATION);
            alert.setTitle("Database offline");
            alert.setHeaderText("De MySQL database server is offline");
            alert.setContentText("Start de server en probeer opnieuw.");

            ButtonType buttonTypeOne = new ButtonType("Opnieuw controleren");
            ButtonType buttonTypeCancel = new ButtonType("Exit", ButtonData.CANCEL_CLOSE);

            alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeCancel);

            Optional<ButtonType> result = alert.showAndWait();
            if (result.get() == buttonTypeOne){
                mysqlStatus = false;
                alert.close();
            } else {
                System.exit(0);
            }
        } catch (Exception e) {
            mysqlStatus = false;
            ExceptionDialog cexD = new ExceptionDialog();
            cexD.setStrError("Er is een onverwachte fout opgetreden.");
            cexD.setException(e);
        } finally {
            try {
                conTest.close();
            } catch (SQLException e) {
                mysqlStatus = false;
                ExceptionDialog cexD = new ExceptionDialog();
                cexD.setStrError("Er is een onverwachte fout opgetreden.");
                cexD.setException(e);
            }
        }

    }

4 Answers 4

3

You don't do

mysqlStatus.equals(false)

in java. Not sure what that will do.

You need to do

(!mysqlStatus)

for checking conditions.

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

5 Comments

Changed it and it still doesn't loop.
@Niell How do you know it does not loop? Do not forget that you put your variable to true within the try clause.
Did you test that though? It's not normal, but it should still work.
That is other problem @Niell
I removed all the code where I put it to true before I tested it. I'm running this code in a constructor of an object if it matters?
0

Any Exception besides an SQLException like ClassNotFoundException will be catched by Exception and loop endlessly. There call break or such.

Boolean is the Object wrapper class for the primitive type boolean.

boolean mysqlStatusVastgesteld = false;

while (!mysqlStatusVastgesteld) {

Comments

0

From here:

The java.lang.Boolean.equals(Object obj) returns true if and only if the argument is not null and is a Boolean object that represents the same boolean value as this object.

now false is a primitive boolean literal (which is either true or false and not Boolean object. Boolean object can be true, false, or NULL)

So, what happen when you do:

mysqlStatus.equals(false)

is you compare a Boolean object mysqlStatus with boolean primitive literal false (and not Boolean object with false value) and therefore it always returns false (because mysqlStatus is not the same Boolean object as false) and as a result you never enter the while loop.

Do this to compare if mysqlStatus is false with Boolean object:

while(Boolean.FALSE.equals(mysqlStatus)) //also handle NULL

Comments

0

I fixed it by making a function that checks the status.

    public Boolean checkStatus() {

        try {
            Class.forName(driver).newInstance();
            Connection conTest = DriverManager.getConnection(host + dbName, username, password);
            return true;
        } catch (Exception e) {
            // server offline
            return false;
        }

    }

I also made a while loop that runs this function until it returns true. If it returns false a dialog appears and asks to retry or exit.

All works now. This was a weird issue but thanks everyone for trying to help! There were multiple mistakes in my code.

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.