Re: Global Information Store between resources

Hi Anselm,

Thanks for your reply.

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

The following is my GlobalResource class:

public class GlobalResource extends HTTPResource {

  protected static int ATTR_SOMEATTR = -1;

  static {
    Attribute attr = null;
    Class     cls  = null;

    try {
      cls = Class.forName("w3c.jigsaw.karen.GlobalResource");
    } catch (Exception ex) {
      ex.printStackTrace();
      System.exit(1);
    }

    // Register the attributes.
    attr = new StringAttribute("Some Attribute", "Default",
                               Attribute.EDITABLE);
    ATTR_SOMEATTR = AttributeRegistry.registerAttribute(cls, attr);
  }

  public String getAttr() {
    return getString(ATTR_SOMEATTR, null);
  }
}

And the stack trace:

java.lang.NullPointerException
        at w3c.jigsaw.karen.ResourceManager.getGlobalResource(ResourceManager.ja
va:17)
        at w3c.jigsaw.karen.HtmlPage.<init>(HtmlPage.java:17)
        at w3c.jigsaw.karen.LoginPage.<init>(LoginPage.java:14)
        at w3c.jigsaw.resources.AttributeHolder.unpickle(AttributeHolder.java)
        at w3c.jigsaw.resources.ResourceIndex.loadResource(SimpleResourceStore.j
ava)
        at w3c.jigsaw.resources.SimpleResourceStore.loadResource(SimpleResourceS
tore.java)
        at w3c.jigsaw.resources.DirectoryResource.lookupStore(DirectoryResource.
java)
        at w3c.jigsaw.resources.DirectoryResource.lookupStore(DirectoryResource.
java)
        at w3c.jigsaw.resources.DirectoryResource.lookup(DirectoryResource.java)

        at w3c.jigsaw.resources.DirectoryResource.lookup(DirectoryResource.java)

        at w3c.jigsaw.resources.DirectoryResource.lookup(DirectoryResource.java)

        at w3c.jigsaw.http.httpd.perform(httpd.java)
        at w3c.jigsaw.http.Client.processRequest(Client.java)
        at w3c.jigsaw.http.Client.loop(Client.java)
        at w3c.jigsaw.http.Client.runConnection(Client.java)
        at w3c.jigsaw.http.Client.run(Client.java)
        at java.lang.Thread.run(Thread.java)

Thanks,
Karen

Received on Tuesday, 28 January 1997 11:11:04 UTC