So my problem is this. I have a class called Globals that contains all the variables I need all over the program. It works fine for things like strings, integers and other things that you can use the = operation on. For example:
public class Globals {
public static int globalInteger = 23;
public static String globalString = "Hello, dude.";
public static UserProfile globalUserProfile;
}
Somewhere in the project, I access them using these:
Globals.globalInteger += 17;
Globals.globalString = "Why, hello there!";
However, I am trying to make a class that I wrote myself global (UserProfiles.class). This class does not use the = operation, hence, it is always null when I access it from somewhere else and I get java.lang.nullPointerException. For example if I do this (newProfile(String) is a method inside UserProfiles.class):
Globals.globalUserProfile.newProfile(profileName);
I get java.lang.NullPointerException. How can I make my UserProfile.class variable accessible all throughout the project? Thanks in advance.
Try Singleton design pattern.
http://en.wikipedia.org/wiki/Singleton_pattern
Examples of GoF Design Patterns in Java's core libraries