Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > static initializer not working

Reply
Thread Tools

static initializer not working

 
 
mike
Guest
Posts: n/a
 
      04-20-2012
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(Plugin Impl.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);
}

}



 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      04-20-2012
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(Plugin Impl.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
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
When gets the static initializer of a private static class exectuted? joes Java 3 01-09-2008 02:56 PM
Class.getMethod in class's static initializer block chucky Java 14 08-02-2007 12:22 PM
Class throwing exceptions in aggregate static initializer causes abort() ... Marvin Barley C++ 6 02-19-2007 11:07 AM
static initializer order dewdman42@gmail.com C++ 6 01-25-2006 08:17 AM
static initializer vs constructor Ed Thompson Java 9 10-11-2004 10:27 AM



Advertisments