Thursday 15 September 2011

Java Singleton.getInstance() returns null? -


I have this singleton that I am trying to use but can get apparently returning empty Are:

  class singleton {public static final string K_LEVEL = "level"; Stable singleton example = new singleton (); Private int level; Static singleton yield (return example;) int getLevel () {return level;} zero incrementLevel () {System.out.println ("increase in LEVEL" +++ level);} zero addToLevel (int x) {for (Int i = 0; i & lt; x; i ++) incrementLevel ();}} class A {public static zero main (string [] args) {singleton s = singleton.just instance (); integer i = integer GateInteger (singleton.kellev); s.addToLevel (i);}}   

I have heard that singlantans in Java are very difficult to execute and run I am in danger of situations. Is my singleton pattern wrong? I have recently changed my code to look like this, and now why is instant return returns blank? A- There is nothing wrong with your singleton: / div>

There are no concurrency problems because it is not a multithrred code. < P> You were thinking that s was zero, but this is actually i this blank ./P>

Since addToLevel takes int as a parameter, therefore integer I was autounboxed (contained from Integer to int ), but since i the null , NullPointerException was thrown off Was there. Autounboxing throws NullPointerException when the value is being hidden null .

Reason Integer.getInteger (Singleton.K_LEVEL) Back null is because you have the Java A-DelWell = 1 The opposite was java -Delvel = 1 A . The latter is the correct syntax.

No comments:

Post a Comment