static initializer not working

Discussion in 'Java' started by mike, Apr 20, 2012.

  1. mike

    mike Guest

    Hi,


    I cannot get my registration of parser to work. When try to make a
    call to get the registered parser:

    PullParserFactory.instance().createParser(type);

    I get a NullPointerException. When I debug I can see that the HashMap
    is empty.

    What am I missing?

    br,

    //mike

    public class FindBugsParser implements AbstractPullParser{

    // Register with Factory
    static {
    PullParserFactory.instance().registerParser(PluginImpl.FINDBUGS,
    new FindBugsParser());
    }

    public AbstractPullParser createParser() {
    return new FindBugsParser();
    }

    }

    public final class PullParserFactory {

    /**
    * Single instance created upon class loading.
    */
    private static PullParserFactory INSTANCE = new PullParserFactory();

    private static final HashMap<String, AbstractPullParser>
    registeredParsers = new HashMap<String, AbstractPullParser>();

    //no outside access.
    private PullParserFactory() {

    }

    public static PullParserFactory instance() {
    return INSTANCE;
    }

    public void registerParser(String type, AbstractPullParser parser) {
    registeredParsers.put(type, parser);
    }

    public AbstractPullParser createParser(String type) {

    return (AbstractPullParser) registeredParsers.get(type);
    }

    }
    mike, Apr 20, 2012
    #1
    1. Advertising

  2. mike

    Eric Sosman Guest

    On 4/20/2012 8:10 AM, mike wrote:
    > Hi,
    >
    >
    > I cannot get my registration of parser to work. When try to make a
    > call to get the registered parser:
    >
    > PullParserFactory.instance().createParser(type);
    >
    > I get a NullPointerException. When I debug I can see that the HashMap
    > is empty.
    >
    > What am I missing?


    My hunch is that nothing has caused the FindBugsParser class to
    be initialized at the time you make your call and get the NPE:

    - The HashMap is initially empty, and gets populated by calls
    to registerParser().

    - The only (visible) call to registerParser() occurs during the
    initialization of FindBugsParser.

    - So if FindBugsParser has not yet been initialized at the time
    of the call to instance(), the HashMap will still be empty and
    instance() will return null.

    The fact that FindBugsParser.class is hanging around somewhere in
    your class path is not enough to make Java load and initialize it;
    Java won't load the class until it sees a need for it.

    > br,
    >
    > //mike
    >
    > public class FindBugsParser implements AbstractPullParser{
    >
    > // Register with Factory
    > static {
    > PullParserFactory.instance().registerParser(PluginImpl.FINDBUGS,
    > new FindBugsParser());
    > }
    >
    > public AbstractPullParser createParser() {
    > return new FindBugsParser();
    > }
    >
    > }
    >
    > public final class PullParserFactory {
    >
    > /**
    > * Single instance created upon class loading.
    > */
    > private static PullParserFactory INSTANCE = new PullParserFactory();
    >
    > private static final HashMap<String, AbstractPullParser>
    > registeredParsers = new HashMap<String, AbstractPullParser>();
    >
    > //no outside access.
    > private PullParserFactory() {
    >
    > }
    >
    > public static PullParserFactory instance() {
    > return INSTANCE;
    > }
    >
    > public void registerParser(String type, AbstractPullParser parser) {
    > registeredParsers.put(type, parser);
    > }
    >
    > public AbstractPullParser createParser(String type) {
    >
    > return (AbstractPullParser) registeredParsers.get(type);
    > }
    >
    > }
    >
    >
    >



    --
    Eric Sosman
    d
    Eric Sosman, Apr 20, 2012
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ed Thompson

    static initializer vs constructor

    Ed Thompson, Oct 9, 2004, in forum: Java
    Replies:
    9
    Views:
    907
    Chris Uppal
    Oct 11, 2004
  2. Replies:
    6
    Views:
    367
    Gabriel
    Jan 25, 2006
  3. Marvin Barley
    Replies:
    6
    Views:
    386
    Marvin Barley
    Feb 19, 2007
  4. chucky
    Replies:
    14
    Views:
    1,273
    Twisted
    Aug 2, 2007
  5. joes
    Replies:
    3
    Views:
    297
Loading...

Share This Page