:$
Ok people, bare with me for a second (or 60...)

I'm developing a part of a larger application, and to cut to the chase, i now need to access an Hashtable that I've made global. This aplication is multithreaded, but I'm trying to access this hashtable from the same thread (or so i think...).
When i run the main class, i create all the Instances i need (one of the Global class also, where I'm creating a new hashtable, later it will retrieve it from a file). When creating this new hashtable i immediately print it's size and as it should, it's 0.

On another class, when i try to access this hashtable using the following code:

if(global.getHist().size()>=0)

i get this error

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at ....HelpingHandSearch.Search(HelpingHandSearch.java:78)
	at ...gui.HelpingHandGui.searchButtonActionPerformed(HelpingHandGui.java:192)
	at ....gui.HelpingHandGui.access$000(HelpingHandGui.java:30)
	at ....gui.HelpingHandGui$1.actionPerformed(HelpingHandGui.java:237)

and it goes on, but if u think it's relevant please let me know and i'll post the rest.

If i take out that line, or any try of accessing that hashtable or any other thing from the Global class it works just as it should...

Any ideas? I'm getting frustrated with this, as I know it's probably some dumb mistake but I'm running out of time :)

Dani AI

Generated

Most likely causes: the reference to the shared Global object is null, or the Global.getHist() method is returning null (so calling a method on it throws a NullPointerException). Because the exception originates from the GUI event thread, the problem is usually an initialization/timing or “different instance” bug rather than a Hashtable implementation problem.

A quick, non-invasive way to confirm which reference is null is to log or break on the event handler and inspect both the Global reference and the map it returns. As suggested, a breakpoint is useful — extend that by printing identity information so the instance created in main can be compared with the instance seen in the handler:

if (global == null) {
  System.err.println("global == null");
} else {
  System.err.println("global id=" + System.identityHashCode(global)
      + " hist==" + (global.getHist() == null ? "null" : "present"));
}

If the map is null, ensure it is initialized in the Global constructor or at declaration so getHist never returns null. If multiple Global objects are being created, centralize access by passing the same instance into the GUI or by using a single shared instance. Example pattern:

public class Global {
  private static final Global INSTANCE = new Global();
  private final Map<String,Object> hist = new Hashtable<>();
  private Global() {}
  public static Global getInstance() { return INSTANCE; }
  public Map<String,Object> getHist() { return hist; }
}

Notes and cautions: testing for size >= 0 is redundant because size() is always non-negative; prefer a null-safe emptiness check (or ensure getHist never returns null). If multiple threads will mutate the map, Hashtable is synchronized but ConcurrentHashMap is usually a better choice for modern concurrent code. Finally, make sure GUI initialization and hand-off of the Global reference happen before the event thread processes user actions.

I would suggest putting a braekpoint on that line to see what is being accessed that is null. Once you find that you might be able to get to the source of the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.