10

I'm debugging on android, and I have the following code. The istrue variable has the value true, but it seems that the if condition doesn't work.

String ok = objRestMan.getMensaje();
boolean istrue = ok.equals("ok");
if(istrue){
    return antenaInformation;
}else{
    return null;
}

As you can see in the next image, the istrue variable is true, but the if condition always takes the route else and returns null.

enter image description here

azurefrog
  • 10,785
  • 7
  • 42
  • 56
mavi
  • 1,074
  • 2
  • 15
  • 29
  • What happens if you change it to `ok.toLowerCase().equals("ok");` – The Head Rush Apr 13 '16 at 17:40
  • 11
    Perhaps a more likely explanation is that `antenaInformation` is `null`. – azurefrog Apr 13 '16 at 17:40
  • 3
    If `istrue` has the value `true`, it will definitely branch to `return antenaInformation;` Could it be that `antenaInformation` is `null`? – T.J. Crowder Apr 13 '16 at 17:41
  • 3
    Assuming that the dark blue line represents the current location of the execution cursor, note that if the compiler or JIT can determine that `antenaInformation` will **always** be `null` at that point in the code, it may well leave out the branch entirely. Also note that debuggers are not absolutely 100% perfect, sometimes (particularly when there's optimization going on), they can show you something somewhat misleading. – T.J. Crowder Apr 13 '16 at 17:45
  • 3
    Is it possible, you changed code, but didn`t recompile program? Or program is remote to debugger, and binary does not match new code changes? – Ivan Apr 13 '16 at 17:46
  • 3
    Well, why not hardcode the return value of `antenaInformation` to `"testing"` and see if the `if` condition is really "not working" – Petro Apr 13 '16 at 17:46
  • 2
    I'm intrigued by the green underline on `istrue`. What does it say when you hover it? – T.J. Crowder Apr 13 '16 at 17:47
  • 2
    (Ignore the downvoter, they clearly didn't look at the image before downvoting. You've clearly spent time trying to figure this out, and you've included relevant information.) – T.J. Crowder Apr 13 '16 at 17:48
  • 2
    Two explanations I can think of are either you are debugging old code as mentioned by someone else or you first did get into the `true` block of the condition and then single stepped the code which jumped to the else block. I noticed Android Studio tends to jump to the final return when you return earlier. Check the actual value returned from this method to see if that is the case as it will probably return the correct value in `antenaInformation` assuming that is not also `null`. – George Mulligan Apr 13 '16 at 17:53
  • Are you sure that you have the right version of the sources? – Nicolas Filotto Apr 13 '16 at 17:53
  • 1
    @T.J.Crowder The green underline is likely warning the OP of a type in the word `istrue`. I put a similar expression in Android Studio and that is what it said. It likes `isTrue`. – George Mulligan Apr 13 '16 at 18:03
  • Hi, thanks for the help. I already clean the project and build it. antenaInformation is not null, it has information. And if i change the if statement to "if(true)" , then it goes to return antenaInformation. For some reason the istrue variable is not equal to true, but as you can see in the image it is true. – mavi Apr 13 '16 at 18:05
  • 3
    Did you check the actual return value of the method? I have a feeling you might be running into this [issue](https://code.google.com/p/android/issues/detail?id=34193) if you cleaned and reran your project. You can also put a breakpoint on the `return antenaInformation;` line and see if it is hit. If it isn't hit then this doesn't explain it. – George Mulligan Apr 13 '16 at 18:07
  • i read about the issue you mentioned. (George Mulligan) And the returning value is the correct. Thanks to all – mavi Apr 13 '16 at 18:24
  • 1
    Good news is the last comment on the issue says it is fixed in Jack once that is officially released. – George Mulligan Apr 13 '16 at 18:26
  • Thanks again George Mulligan – mavi Apr 13 '16 at 19:12
  • @mavi You're welcome. I was also confused the first time I saw this behavior and thanks to your question I finally understand the reason it happens after doing a bit of searching. – George Mulligan Apr 13 '16 at 19:37

0 Answers0