The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
February 2002

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Problems with Static references

Posted by Tim on February 22, 2002 at 11:01 AM

Sorry about the previous post, I accidently hit "post" half way through...

I'm trying to implement a class that uses a static method to return an instance of itself to the calling object. It's not precisely a singleton because it can also be instantiated via a call to a constructor.

I've gotten it to work more or less correctly, but I'm having trouble with methods in the instance that access non-static variables:

The pertinent code follows:

public class GeneManager {

private GeneManagerGUI gui;

private static GeneManager manager;
private static boolean initialized = false;


public GeneManager() {
this.manager = this;
initialized = true;
}

public void setGUI(GeneManagerGUI gui) {
this.gui = gui;
manager.setGUI(gui);
}

public static GeneManager getGeneManager() {
if (!initialized) {
manager = new GeneManager();
initialized = true;
}
return manager;
}


What I'm doing is the following:

From an object that doesn't have a reference to the GeneManager, I call getGeneManager to get reference to the one instance of the GeneManager. Later on, I use that instance to create a GeneMangerGUI display of the Manager (represented by the non-static variable gui above), which calls setGUI in the manager, giving GeneManager a reference to the Gui that displays its information.

However, when I call any methods in the GeneManager that reference the instance of gui, I get a NullPointerException.

For example:

A call to...

public void newGroup(String sourceName) {
setName(sourceName);
gui.selectNewEntry(sourceName);
}

throws a NullPointerException when gui.selectNewEntry is called.

I would really appreciate any help that anyone can give. I'm a bit unfamiliar with static methods and variables, and hope someone can give me a hand.

Thanks,

Tim





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us