Results 1 to 6 of 6
  1. #1
    gligor_kot is offline Member
    Join Date
    Jul 2009
    Posts
    7
    Rep Power
    0

    Default If statement not executing correctly

    Hi everyone,

    I am a new member of this forum and I need your help for a little problem I have.

    I posted this in the Java2D part of this forum because my program is connected with displaying stuff on the screen, but I didn't have luck in there, and I also think this part of the forum better serves the problem I am asking help for. I couldn't link my previous post because I cannot post url's just yet.

    I have this piece of code in my main function:

    Java Code:
    while (true) {
       if (simRunning) {
          da.repaint();
          fpsGA.repaint();
          vehiclesNumberGA.repaint();
       }
    }
    The purpose of the code is to update the display area, and a few graph areas while my simulation is running. This worked perfectly for a while, but recently I think I've updated to a newer version of JDK and now the program doesn't seem to run the repaints if my simRunning boolean is true.

    What is even more interesting, when I go to debug mode it does run the repaints when my simRunning is true. if I run my program normally, even when simRunning is true, the execution never enters the body of the "if" statement and never executes my repaints...

    Any suggestion will be appreciated.

    Thanks,

    Gix

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default print outs in the code

    Have you tried putting println's in the code to see the boolean values and the flow of the code to see if it is really entering that segment?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    gligor_kot is offline Member
    Join Date
    Jul 2009
    Posts
    7
    Rep Power
    0

    Default

    Yep. That's the first thing I did. I thought maybe I am not changing the boolean value, but when I put something like:
    Java Code:
     System.out.println(simRunning);
    I get "true" printed all the time while in the loop (which is obviously infinite), but the program execution still never enters the if statement, even though simRunning is clearly TRUE.

  4. #4
    Hardik is offline Member
    Join Date
    Jul 2009
    Posts
    6
    Rep Power
    0

    Default

    You check your code where "simrunnig" data type is boolean or not bcoz it never goes into the if statement if your data type is anything instead boolean. :)

  5. #5
    gligor_kot is offline Member
    Join Date
    Jul 2009
    Posts
    7
    Rep Power
    0

    Default

    hehe. Yeah simRunning is a boolean. And as I have stated up there, it works perfectly when I run it in debug mode... only problem is if I do a normal execution of the program.

    I am still thinking it has something to do with the way threads are processed under the updated JDK. But I still cannot get it working.

    I probably need to also mention I am running this under Ubuntu Linux 9.04.

  6. #6
    gligor_kot is offline Member
    Join Date
    Jul 2009
    Posts
    7
    Rep Power
    0

    Default

    OK. New day, old solution works... I get a pretty strange result so I decided to post it and see what you guys think, and it may also help some people if they have similar issues.

    Here it goes. When I use this piece of code (as mentioned in my first post here) my display areas and graphs do not repaint even though the simRunning boolean is "true":
    Java Code:
    while (true) {
    			if (simRunning) {
    				da.repaint();
    				fpsGA.repaint();
    				vehiclesNumberGA.repaint();
    			}
    		}
    So, I added a line of code just to test and make sure again that simRunning is "true", and the execution of the program entered the if statement!? I added the bolded line of code:

    Java Code:
    while (true) {
    			[B]System.out.println(simRunning);[/B]
    			if (simRunning) {
    				da.repaint();
    				fpsGA.repaint();
    				vehiclesNumberGA.repaint();
    			}
    		}
    We discussed it with a colleague of mine, and the only reasonable explanation we could find is that there is something connected to the JIT (just in time) compiler that it dismisses the variable for some strange reason, but then when I add a line for println the value of simRunning has to be accessed and thus the compiler knows it has changed and enters the loop immediately after.

Similar Threads

  1. Gueesing Game Almost done, but not working correctly
    By mbnumba6 in forum New To Java
    Replies: 5
    Last Post: 03-18-2009, 03:01 AM
  2. Help with sphere.java - not coming out correctly
    By JavaInLove in forum Java 2D
    Replies: 3
    Last Post: 03-17-2009, 08:38 PM
  3. Replies: 2
    Last Post: 07-18-2008, 01:13 PM
  4. [SOLVED] \t not working correctly?
    By Gakusei in forum New To Java
    Replies: 5
    Last Post: 05-06-2008, 04:45 PM
  5. How Do I Embed Java Correctly To A Web Page
    By abcd in forum Java Applets
    Replies: 7
    Last Post: 01-28-2008, 07:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •