getClassLoader()

Godmar Back gback at cs.utah.edu
Tue Jul 28 08:54:22 PDT 1998


> 
> 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



More information about the kaffe mailing list