Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #13686

Re: static initializer not working

Path csiph.com!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail
From Eric Sosman <esosman@ieee-dot-org.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: static initializer not working
Date Fri, 20 Apr 2012 08:42:53 -0400
Organization A noiseless patient Spider
Lines 86
Message-ID <jmrlkh$a41$1@dont-email.me> (permalink)
References <2c2614fe-33a4-457e-a0c0-e44838caf220@m16g2000yqc.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Fri, 20 Apr 2012 12:42:57 +0000 (UTC)
Injection-Info mx04.eternal-september.org; posting-host="HSlJAUb3pGXi3i7ZL/HoAw"; logging-data="10369"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18+HfDkWA8MhR4jko2x/IKH"
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20120327 Thunderbird/11.0.1
In-Reply-To <2c2614fe-33a4-457e-a0c0-e44838caf220@m16g2000yqc.googlegroups.com>
Cancel-Lock sha1:coZ8MesMs8x58CLBHsJaghoY9Rc=
Xref csiph.com comp.lang.java.programmer:13686

Show key headers only | View raw


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
esosman@ieee-dot-org.invalid

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

static initializer not working mike <mikaelpetterson@hotmail.com> - 2012-04-20 05:10 -0700
  Re: static initializer not working Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-04-20 08:42 -0400

csiph-web