I already did research on this issue, and also tried to figure it out myself, but no luck. So I decided to ask a question.
Basic information:
There are two classes. FBClient and State . In FBClient , I have a static variable of this type, fbc , a StateManager instance that has several methods for working with State , some constants and two getters. In State , I'm trying to initialize a BufferedImage .
public class FBClient { //Static public static FBClient fbc; //In init method private StateManager stateManager; //Constants private final int INIT_FRAME_WIDTH = 320, INIT_FRAME_HEIGHT = (INIT_FRAME_WIDTH / 4) * 3, SCALE = 3, FRAME_WIDTH = INIT_FRAME_WIDTH * SCALE, FRAME_HEIGHT = INIT_FRAME_HEIGHT * SCALE; public static void main(String[] args) { try { //First call in exception chain: fbc = new FBClient(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } private FBClient() throws IOException { //Second call in exception chain: init(); } private void init() throws IOException { stateManager = new StateManager(); //Third call in exception chain: stateManager.addState(new MainMenu((byte) 0, "Main Menu")); //MainMenu is the subclass of State, and the constructor just calls "super(0, "Main Menu")" } public int getFRAME_HEIGHT() { return FRAME_HEIGHT; } public int getFRAME_WIDTH() { return FRAME_WIDTH; } } public abstract class State { protected final byte ID; protected final String NAME; protected final BufferedImage SCREEN; protected final Graphics2D GRAPHICS; public State(byte id, String name) { this.ID = id; this.NAME = name; //Exception cause: this.SCREEN = new BufferedImage(FBClient.fbc.getFRAME_WIDTH(), FBClient.fbc.getFRAME_HEIGHT(), BufferedImage.TYPE_INT_RGB); this.GRAPHICS = SCREEN.createGraphics(); } }
Additional Information:
If I put literals in a BufferedImage initialization, it works.
If I initialize two variables in the State class, assigning them literals and putting these variables in the initialization, it works.
If instead of assigning literals to these variables, I assign them to FBClient.fbc.getFRAME_WIDTH() and FBClient.fbc.getFRAME_HEIGHT() , it throws a NullPointerException .
If I create the System.out.println(getFRAME_WIDTH + " : " + getFRAME_HEIGHT) class System.out.println(getFRAME_WIDTH + " : " + getFRAME_HEIGHT) in FBClient , it prints correctly, but if I do it in the State class (and without adding FBClient.fbc. Before it), it throws a NullPointerException .
If I create the constants FRAME_WIDTH and FRAME_HEIGHT public , and I try to access them from the State class by running FBClient.fbc.FRAME_WIDTH and FRAME_HEIGHT , it throws a NullPointerException .
If I try to access the constants from the FBClient class directly, and not to the getters, it still prints correctly.
Finally
Thank you for taking the time, and if you need more information for work, ask me in the comments and I will provide it. In addition, I apologize if the question is not well built / not well explained. If so, tell me how I can improve it. In addition, I apologize if this question was asked and already answered once, maybe I missed it, but, as I said, I did my research.
Edit # 1
Suggested comment I am printing out the fbc value to see if it is null. So I added this line of code to the State constructor: if(FBClient.fbc != null) System.out.println("Not null"); else System.out.println("Null"); if(FBClient.fbc != null) System.out.println("Not null"); else System.out.println("Null"); And, as suspected, he printed a zero. Why is this? I clearly initialized the variable in the main method ...