Re: Global Information Store between resources

Karen Cheng writes:
 > Using getParent() within getSomeResource() means that this method
 > can't be made static, and so we'd have to create an instance of
 > MyResource before we can get the shared context resource, right?

Correct,

 > I have a GlobalResource class which registers a number of global 
 > attributes, and then I have the following ResourceManager class (which
 > is your MyResource class):
 > 
 > public class ResourceManager extends HTTPResource {
 >   static GlobalResource gr;
 > 
 >   public ResourceManager() {}
 > 
 >   public synchronized GlobalResource getGlobalResource() {
 >     if (gr == null) {
 >       // Get the container resource.
 >       Resource parent = getParent();
 >       // Lookup the global resource within that container.
 >       gr = (GlobalResource) ((DirectoryResource) parent).lookup("globalResource\
 > ");
 >     }
 >     return gr;
 >   }
 > }
 > 
 > However, when I tried to use it in the constructor of one of my other 
 > classes, I get a null pointer exception from getGlobalResource():
 > 
 >   public MyNewResource() {
 >     ResourceManager rm = new ResourceManager();
 >     GlobalResource gr = rm.getGlobalResource();
 > 
 >     String attr = gr.getAttr();
 > 	:	:	:
 >   }
 > 
 > Note that I've used Admin/Editor to create "globalResource" under the User
 > directory, which is where all my other resources were created.
 > 
 > Could you please tell me what's wrong?  Why didn't lookup() load the
 > global resource when it couldn't find it in the resource store?

It should have, can you provide the stack trace for the
NullPointerException ? I think either the globalResource doesn't
exist, or it hasn't been initialized properly; the stack trace will
help deteriming the precise cause of the problem.

Anselm.

Received on Tuesday, 28 January 1997 03:25:34 UTC