0

I'm testing this function, but it just doesn't want to return true. Here it is:

public boolean linesExist(){
    return lines != null ? !lines.isEmpty() : false;
}

Just checks whether or not an arraylist has elements in it, pretty simple.

However, even when all the values are correct this function returns false. I have refactored it to the following for easier debugging, but the results are even more strange:

public boolean linesExist(){
    if (this.lines != null) {
        boolean linesExist = !this.lines.isEmpty();
        return linesExist;
    } else {
        return false;
    }
}

enter image description here https://i.stack.imgur.com/rYdNT.gif

Here is a gif going line by line through the function, the bottom has the related values (they're also displayed next to the code while its running). As you can see, it goes into the first if, then hits the "return true" and THEN goes into the else to hit the "return false"

I'm stumped, if anyone has a suggestion on what to do that'd be great.

EDIT: forgot to post the gif, sorry. https://i.stack.imgur.com/rYdNT.gif

[FINAL EDIT]: the issue was with the ide, clean the build, restart the ide and everything should work

swerly
  • 2,015
  • 1
  • 14
  • 14
  • 1
    How are you testing this? I don't know if you're showing enough code and information for us to be able to help yet. – Hovercraft Full Of Eels Aug 13 '16 at 20:03
  • Are you sure that `lines` are not empty each time you calling method? – ByeBye Aug 13 '16 at 20:07
  • Please show the code that initializes `lines` and calls `linesExist()`? – Code-Apprentice Aug 13 '16 at 20:10
  • Sorry I forgot to post the gif. I'm literally just calling this function on an object that has an arraylist "lines" as a private member. I'm sure that lines is not empty, as can seen by the gif the size is 1. – swerly Aug 13 '16 at 20:12
  • 1
    Unable to reproduce. See [IDEONE](https://ideone.com/bHDAlE). – Andreas Aug 13 '16 at 20:20
  • I've seen weird line numbers in the debugger of android before, is the returned value correct? – k5_ Aug 13 '16 at 20:24
  • @k5_ I think you're onto something. Even though its skipping through some weird line stuff I think it's returning true. Now I'm thinking the issue is outside of this function, something to do with Float.NaN – swerly Aug 13 '16 at 20:29

2 Answers2

2

Sometimes inverting logic makes stuff much easier to read/understand.

boolean linesExist() {
   if (lines == null) return false;
   if (lines.isEmpty()) return false;
   return true;
}

Maybe that helps.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 1
    I don't think that solves the problem which author has... – ByeBye Aug 13 '16 at 20:08
  • I agree that this probably doesn't actually answer the question. However, the logic is much simpler without negating the `if` conditions. – Code-Apprentice Aug 13 '16 at 20:09
  • 1
    It might; by indirection: maybe he is just "blindsided" by the "inherent" complexity of his code; and maybe this helps him to get to the real, underlying problem. – GhostCat Aug 13 '16 at 20:09
  • I appreciate your comment, but it's not that I'm not understanding my code, it's that it is operating in a way that I'm seeing as incorrect. (the gif shows the weird behavior) – swerly Aug 13 '16 at 20:24
1

This is not a problem of the code, it cannot execute both the 'if' and the 'else' statements. Try rebuilding your project.

Addis
  • 2,480
  • 2
  • 13
  • 21
  • yeah that's why I found this strange. The problem was somewhere else in my code though. Apparently you're only supposed to use Float.NaN to check for NaN values, not to set variables to. – swerly Aug 13 '16 at 20:35
  • @swerly, I don't see how any code change corrected this, I believe that the 'if' & the 'else' statements cannot be executed both even by error, as far as they are 'coupled'. It's glad you get a way, anyway. – Addis Aug 13 '16 at 20:45
  • I think it was an issue of Android Studio bugging out and displaying wrong line numbers, like another user mentioned. – swerly Aug 13 '16 at 21:39
  • And how is it solved? Just for others who encounter the same problem. I personally have come across with the same problem a number of times, and solved it by rebuilding the project. – Addis Aug 13 '16 at 21:59
  • I didn't fix the issue with Android Studio, just let it be until I run into it again. I had an error elsewhere in my code around where I was using this function that was the issue. – swerly Aug 14 '16 at 15:09