0

I have something like

public class Toys {

public static Toy BAT = Registry.getToy("BAT");
public static Toy DOLL = Registry.getToy("DOLL");

}

where Registry keeps tracks of all the objects of type Toy. When provided with a key, it will return with the valid Toy instance.

When I do, Toys.BAT, it gives me null value, but Registry.getToy("BAT") returns me with valid Toy instance.

public class Registry {

private static final HashMap<String, Toy>  _toysMap = new HashMap<String, Toy>();


public static void putToy( String toyCode, Toy toy) {
    _toysMap.put( toyCode, Toy ) ;
}

public static Toy getToy(String toyCode ) {
    return _toysMap.get( toyCode);
}

}

Is there anything very obvious that I missing ?

Also , I am calling Registry.getToy("BAT") and Toys.getToy("BAT") at the same time ..

Here's the TestRegistry which fails

import static org.junit.Assert.*;

import org.junit.Before; import org.junit.Test;

public class TestRegistry {

@Before
public void setUp() throws Exception {
    System.out.println(Toys.BAT);
    Registry.putToy("BAT", new Toy());
    Registry.putToy("DOLL", new Toy());
}

@Test
public void test() {
    System.out.println(Registry.getToy("BAT"));
    System.out.println(Toys.BAT);
    assertTrue(Registry.getToy("BAT") == Toys.BAT);
}

}

The above prints

null - Registry doesnt contain the toy

Toy@80cac9 - Registry has the toy now

null - Toys.BAT still has null value .

7
  • 5
    This piece of string I have here in my hands... how long is it? :P Without the full source code for your Registry object, we might as well shake a magic 8 ball.
    – mcfinnigan
    Apr 20, 2012 at 13:48
  • We would need to see Registry and how you are putting things into it. Apr 20, 2012 at 13:48
  • When in the program flow are you calling Toys.BAT? Apr 20, 2012 at 13:50
  • 2
    In all likelihood the code above is being run before the toys have been set in the Registry Apr 20, 2012 at 13:50
  • I have attached the Registry code. Its just a hashmap ..
    – sbose
    Apr 20, 2012 at 14:04

3 Answers 3

2

Surely looking into the code of Registry is necessary to know where the problem is.

But generally, it is not safe to initialize your static variables like that. Maybe at the loadig time of the Toys class the Registry still did not load all the toys entries correctly.

2
  • Say, during initial loading of Toys class , all values were null. If the Registry gets updated later and adds all toy entries .. and Toy.BALL is called then .. will it still return null value ?
    – sbose
    Apr 20, 2012 at 14:11
  • @sbose The static variables are initialized once when the class is loaded. That is usually the first time the class is used. This first time can be a very tricky for an example in a container.
    – Stefan
    Apr 20, 2012 at 15:31
1

did you checked the case of Bat and BAT

2
  • 1
    wait for more details. right now it is pure speculation. Maybe getToy() has an empty body, we don't know.
    – UmNyobe
    Apr 20, 2012 at 13:50
  • this means Registry.getToy("Bat") and Registry.getToy("BAT") are different Apr 20, 2012 at 13:52
0
@Before
public void setUp() throws Exception {
    System.out.println(Toys.BAT); --> Loads class and initializes static variables
    Registry.putToy("BAT", new Toy()); --> populates Registry (HashMap)
    Registry.putToy("DOLL", new Toy()); --> populates Registry (HashMap)
}

You need to initialize the values in the registry before your Toys class gets loaded. I agree with the commenters that said its not a good way to initialize these.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.