getClassLoader()

Godmar Back kaffe@rufus.w3.org
Tue, 28 Jul 1998 09:54:22 -0600 (MDT)


> 
> Alexandre Oliva writes:
> > > The method Class.getClassLoader() is returning null, which is
> > > causing stuff to bomb out with NullPointerException.
> > 
> > getClassLoader() is supposed to return null for classes loaded by the
> > default class-loader.  If things are bombing because it is returning
> > null, the problem must be elsewhere
> 
> Yes, you're right.. here's the problem I'm having:
> 
>   java.lang.NullPointerException
> 	  at java/lang/Class.getResourceAsStream(158)
> 	  at ca/mcgill/sable/sablecc/GenTokens.<init>(91)
> 	  at ca/mcgill/sable/sablecc/SableCC.main(163)
> 	  at SableCC.main(73)
> 
> Because in java/lang/Class.java:
> 
>   public InputStream getResourceAsStream(String name) {
> 	  return (getClassLoader().getResourceAsStream(fullResourceName(name)));
>   }
> 
> So I guess getResourceAsStream() needs to be fixed.
> 

 Yes, I'll do that shortly.

The fix that comes to mind is most likely this:
	
   public InputStream getResourceAsStream(String name) {
	  ClassLoader loader = getClassLoader();
	  if (loader == null) {
		ClassLoader.getSystemResourceAsStream(fullResourceName(name));
	  } else {
	      return (loader.getResourceAsStream(fullResourceName(name)));
	  }
   }

I'm going to scan the libs for more assertions of that kind.
I take responsibility for breaking that; originally, getClassLoader()
would have returned java.util.SystemClassLoader(), which would have worked.

	- Godmar