aspose file tools*
The moose likes Java in General and the fly likes Method stopped working in Program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Method stopped working in Program" Watch "Method stopped working in Program" New topic
Author

Method stopped working in Program

Thomas Crew
Greenhorn

Joined: Nov 13, 2012
Posts: 10
Hi, I'm working on a program that displays different shapes on the canvas with a class called "Driver". I have a method called "toggle" that makes the shapes look like they're randomly twinkling by removing and then bringing back the visibility of a shape in the class called "Star". Unfortunately when i came back to work on the code today, the method "toggle" seems to not work anymore and i can't figure out what went wrong. My guess is i might have unintentionally removed a piece of code or when i added on more code i might've ruined the toggle method is someway. Can someone please help me out and see if you can find the problem? I have a feeling its something minuscule or obvious but i've just about given up on trying to figure it out myself!

Here:




Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
There's not enough code shown to see what is happening ie what draws the shapes? what thread is this run on? etc etc

I suggest you add print statements at various places in your code so you can confirm that toggle is being called and is called at a rate that would allow you to see the twinkle. Also add prints in the drawing code to check the background is being drawn over the star (or however you remove it) etc.

Not that this is of any use to you right now but this type of problem highlights the advantages of using an RCS or at the very least zipping up the code each time you get to a milestone - at least then you can revert back to an earlier working version or use diff to see what you have changed.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2433
    
  28

THis loop



will give an ArrayOutofBoundsException, and the control will not go to the loop after it that is doing the toggling. WHen you have an array of size 20, the index can go from 0 to 19, not from 0 to 20. In the last iteration of the loop, you will be accessing array[20] which will cause an exception

ArrayOutofBoundsException is a runtime exception, and you are not catching it, so control must be going outside of the run method. My guess is that code is terminating the thread somehow and not logging the exception.

Add more logging in your code, and also change the terminating condition on that loop to i<20
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
Well spotted Jayesh. The OP didn't mention any exceptions so I didn't even look for anything that might throw one.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2433
    
  28

Thanks Old code reviewer hand here If I had a dollar for every for loop I caught, I would have umm.. a good education fund for my son
Thomas Crew
Greenhorn

Joined: Nov 13, 2012
Posts: 10
Thanks A LOT Jayesh, you were completely right and now the "twinkling" works again! The way you explained the error made sense too. Appreciate it
 
wood burning stoves
 
subject: Method stopped working in Program