简体   繁体   中英

Java if () statement always false?

In the 20+ years I've been programming in Java, I've never come across anything as puzzling as this. In the following code:

        // Initialize Firebase Auth
    mAuth = FirebaseAuth.getInstance();
    currentUser = mAuth.getCurrentUser();
    if (currentUser == null) {
        LoginManager.getInstance().logOut();
        LoginFragment loginFragment = new LoginFragment();
        FragmentManager manager = getActivity().getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.fragment_container, loginFragment, loginFragment.getTag()).commit();
        return view;
    }
    firebase_userID = currentUser.getUid();

I set a breakpoint at the if() statement and when I get there, I set the variable, currentUser, to null. When I resume execution, the statement LoginManager.getInstance().logOut() should execute, right? Instead, the code falls through to the line after the if(). Why on earth should this happen??

To prove that I'm not dreaming, here are the relevant screenshots: 在此处输入图像描述 and在此处输入图像描述

And yes, I did rebuild and reload the app after adding the if() statement. I even restarted Android Studio.

As per my understanding and experience, when a code is changed dynamically when in debug mode, it tries to run again from the point where breakpoint is given so that purpose of debugging is served and you do not need to trigger it again. Once any exception is thrown or manually current thread execution is made over then it will not come back to if statement. In that case it has to be re triggered again to execute with the changes done.

I moved the declaration of currentUser to inside createView() and the problem went away. I don't understand why that should make a difference, especially since the identical code in a similar app doesn't have this problem and currentUser is declared global.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM