Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (636)
Games in Android Showcase (178)
games submitted by our members
Games in WIP (687)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Puzzled by bug and bug fix  (Read 1284 times)
0 Members and 1 Guest are viewing this topic.
Offline pin_kushn

Senior Newbie

Java games rock!

« Posted 2006-01-20 02:51:27 »

I bumped into a strange bug the other day.  I found a way to fix it, but I have no idea why it works and I'm wondering if someone can explain it.

Here is the situation.  I built a small set of classes to aid in my game development.  One class, "SoundFactory", has only static methods for loading and playing Clips.  It also has two static fields for HashMaps.  I never intended to create an instance of SoundFactory.

The first game that I wrote with the help of SoundFactory worked fine, but the next one did not.  The code simply stopped executing somewhere soon after a call to a SoundFactory static method.  Subsequent Game runs always stopped at the same place, until I commented out a few miscellaneous instantiations.  Then the code consistently stopped at a different spot.  The "stopping spots" always included an instantiation.  No errors were thrown.  Code just stopped executing.

So why did Game1 work and not Game2?  I commented out code until I finally found the difference.  Game1 had the following field declaration/assignment:

Color color = Color.BLUE;

When I added this seemingly irrelevant line as a field for Game2, the code ran fine.

Can anyone explain what I was doing wrong?  I am guessing that I should abandon the whole static idea, but I am still curious about what the hang-up might be.

I code in 1.4 using Eclipse with my Mac, 10.3.



Offline bauerr

Junior Devvie

Java!!!!!!!!!!! !!!

« Reply #1 - Posted 2006-01-20 06:57:56 »

You should post (parts of) your code to get proper help.
Offline pin_kushn

Senior Newbie

Java games rock!

« Reply #2 - Posted 2006-01-20 21:17:47 »

Ok, here is a pared down version of Game and SoundFactory.  The code hangs in the Game constructor on creating a JFrame.  The same hang happened with all sorts of other classes -- it doesn't have anything to do with JFrame.  If I do not call the SoundFactory loadClip method, the JFrame is created.  Most puzzling is this: when I leave in the call to loadClip, but also create a reference to a static constant, everything works fine!??!?  This only works if a actually assign the field to a static constant: just declaring it doesn't work.

Any insights would be appreciated.

public class Game {
   //Color x = Color.BLACK;
   public Game() {
      File b = new File("resources/Boom.wav");
      SoundFactory.loadClip(b, 3);

      System.out.println("About to create JFrame");
      JFrame jf = new JFrame();
      //following line doesn't happen unless I uncomment Color, above
      System.out.println("Created JFrame");

   public static void main (String [] args) {
      new Game ();

public class SoundFactory {

   private static HashMap availableClips;
   static {
      availableClips = new HashMap();

   public static void loadClip (File soundFile, int howManyCopies) {
      try {
         for (int x = 0; x < howManyCopies; x++) {
            InputStream is = new FileInputStream(soundFile);
            loadClip (is, soundFile.getName());
      catch (Exception e) {
   public static void loadClip (InputStream is, String name) {

      if (!availableClips.containsKey(name)) {
         availableClips.put(name, new LinkedList());
      List list = (List) availableClips.get(name);

      AudioInputStream audioInputStream = null;
      try {
         audioInputStream = AudioSystem.getAudioInputStream(is);
      } catch (UnsupportedAudioFileException e1) {
      } catch (IOException e1) {
      AudioFormat   format = audioInputStream.getFormat();
      DataLine.Info info = new DataLine.Info(Clip.class, format);
      Clip clip = null;
      try {
         clip = (Clip) AudioSystem.getLine(info);;
      catch (LineUnavailableException e) {
      catch (IOException e) {
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jeff

JGO Coder

Got any cats?

« Reply #3 - Posted 2006-01-22 18:59:58 »

This really sounds like a race condition as the assignmetn has no effect on the code in  question other then chnaging the timing.

Id have to look closer though to determine exactly what is racing...

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!
Offline pin_kushn

Senior Newbie

Java games rock!

« Reply #4 - Posted 2006-01-24 14:56:09 »

what is a race condition?
Offline Mr_Light

Senior Devvie

Medals: 1


« Reply #5 - Posted 2006-01-24 15:47:18 » its been a while since I read it but I beleave it was in that section, its somewhere in the trail adleast

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Dwinin (70 views)
2015-11-07 13:29:08

Rems19 (80 views)
2015-10-31 01:36:56

Rems19 (72 views)
2015-10-31 01:32:37

williamwoles (106 views)
2015-10-23 10:42:59

williamwoles (92 views)
2015-10-23 10:42:45

Jervac_ (106 views)
2015-10-18 23:29:12

DarkCart (134 views)
2015-10-16 00:58:11

KaiHH (116 views)
2015-10-11 14:10:14

KaiHH (155 views)
2015-10-11 13:26:18

BurntPizza (169 views)
2015-10-08 03:11:46
Rendering resources
by Roquen
2015-11-13 14:37:59

Rendering resources
by Roquen
2015-11-13 14:36:58

Math: Resources
by Roquen
2015-10-22 07:46:10

Networking Resources
by Roquen
2015-10-16 07:12:30

Rendering resources
by Roquen
2015-10-15 07:40:48

Math: Inequality properties
by Roquen
2015-10-01 13:30:46

Math: Inequality properties
by Roquen
2015-09-30 16:06:05

HotSpot Options
by Roquen
2015-08-29 11:33:11 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!