0

Can someone please explain how is possible, that method obtain(..) throws IllegalStateException for input ConfiguratorType.SKODA (the variable configurators contains {SKODA=null})? How can it be null, I do not understand why SkodaConfigurator.INSTANCE returns null. It should never be null or am i mistaken? The code is executed in servlet environment, Java 7.

Thank you

public class CarConfigurators {

    private static Map<ConfiguratorType, CarConfigurator> configurators
          = new EnumMap<ConfiguratorType, CarConfigurator>(ConfiguratorType.class);

    static {
        configurators.put(ConfiguratorType.SKODA, SkodaConfigurator.INSTANCE);
        // ..
    }

    public static CarConfigurator obtain(ConfiguratorType type) {
        CarConfigurator configurator = configurators.get(type);
        if (configurator == null)
            throw new IllegalStateException("Car configurator of type " + type + " is not registered.");
        return configurator;
    }
   ...
}


public class SkodaConfigurator extends CarConfigurator {

     public static final SkodaConfigurator INSTANCE = new SkodaConfigurator();
     ...
}

public enum ConfiguratorType {
    SKODA,
    // ..
}
vernjan
  • 379
  • 3
  • 11
  • 2
    Please post a short but *complete* program demonstrating the problem. We don't know which class you're declaring here, for example... my guess is that it's actually in `CarConfigurator`, in which case I can explain it. – Jon Skeet Jun 18 '14 at 10:33
  • You show a `CarConfigurators` (plural) class, and then `SkodaConfigurator` extends `CarConfigurator` (singular). My guess is this is just a typo but just want to rule out any other possibilities (i.e. there is also a `CarConfigurator` class out there..) Please fix if it's a typo. – sparc_spread Jun 18 '14 at 10:41
  • It is not a typo, CarConfigurators is factory for creating instances of different CarConfigurator subclasses. – vernjan Jun 18 '14 at 10:45
  • Can you post your code for ConfiguratorType? – Ivaylo Toskov Jun 18 '14 at 10:47
  • Added, it's just a simple enum – vernjan Jun 18 '14 at 10:49
  • @vernjan Please read the initial comment: can you post a *complete* example that reproduces the issue? So we can copy/paste/run. – assylias Jun 18 '14 at 11:12
  • Hey, so I have figured it out. The problem was somewhere else. I found out after decompiling the troublesome classes. Somehow (won't go into detail) my compiled class for SkodaConfigurator got corrupted while compiling. So there is nothing wrong the code itself. Sorry for your trouble and thanks for you time. Deleting the question. – vernjan Jun 18 '14 at 11:52

2 Answers2

1

Static code cannot all run simultaneously, the various bits of static initialization going on have to happen in a given order. Clearly in this case, your static block which does configurations.put(...) is running before the static variable in SkodaConfiguration is initialized.

JonathanS
  • 211
  • 1
  • 4
  • 1
    I don't think that can happen. – assylias Jun 18 '14 at 10:37
  • Would you mind to explain it a little bit more please? – vernjan Jun 18 '14 at 10:47
  • It's hard to know without seeing the rest of the code. For example, is there any further code in SkodaConfigurator or CarConfigurator that carries out static initialisation which references static members of CarConfigurators, or in the constructor of SkodaConfigurator? This would mean you had a circular dependency which can cause this sort of issue. – JonathanS Jun 18 '14 at 11:02
0

This is related to static initialization order. I found this from another answer

public class Main {

    {
        System.out.printf("NON-STATIC BLOCK\n");
    }

    static{
        System.out.printf("STATIC BLOCK\n");
    }

    public static Main m = new Main();

    public Main(){
        System.out.printf("MAIN CONSTRUCTOR\n");
    }

    public static void main(String... args) {
        //Main m = new Main();
        System.out.printf("MAIN METHOD\n");

    }
}

Output :

STATIC BLOCK

NON-STATIC BLOCK

MAIN CONSTRUCTOR

MAIN METHOD

Please go through this : Java Static Initialization Order

Community
  • 1
  • 1
tharindu_DG
  • 8,900
  • 6
  • 52
  • 64
  • 2
    If all you're going to do is direct OP elsewhere, it's better to post the link in a comment. Answers should be able to stand by themselves even if links are removed. – awksp Jun 18 '14 at 10:37