• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

If/else statement returns always false, even if the statement is true.

 
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, again.

I got a problem with the if/else statement. The thing returns always the "else" part. It is checking if the value is in the database or not. Funny thing is it works if I do not put inside an if statement, however when I enclose it it skips the if part and goes always to the else part which returns nothing. I use the logic inside an action performed method. Outside of it it seems to works just fine. Is there a problem with the threads or something?

That's the code:



That's an example where the code actually works but is not inside an action listener method:



The method inside the if condition:

Another class:



Why would the if statement work that strange? It always returns "ID does not exist! Try another" part even if without the if/ else condition it works just fine.

Working code:

 
Rancher
Posts: 5008
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried printing out the values used in the expression in the if statement so you can see what is being tested?  The print out could show you why the expression used in the if statement is not true.
 
Marin Capranov
Ranch Hand
Posts: 69
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Solved! I found that the error was in the parameters of the List of the geOnlyById method. The method returned int and I was checking if it contained string value.

That's the result:



if you can see the method initially was returning a List<Integer> and now is List<String>.

The condition is performing on string values:



Hope it clears out!
 
Ranch Hand
Posts: 127
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's pretty obvious from the posted code even without the getIdOnly() method:

vs

It's obvious right away that you have some sort of generic collection but with a different type. If the second one works, but as generics can't be primitives, it had to be some like Collection<Number> - try a contains(String) was just asking for an "unexplainable error".

Aside from all that, the way more severe issue you should fix ASAP: Move your database stuff out of the EDT! What you constructed is what most users know as "the program froze" - you block the EDT with something that can potential takes up a lot of time. As the EDT is the essential part for GUIs in Java it always has to be ensured it runs without any time consuming blocks.
If you have to do something like a database query triggered by the UI you have to de-couple it - and pay attention to re-couple the modification of the UI to only happen back in the EDT:
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic