Note that there are some explanatory texts on larger screens.

plurals
  1. PONullPointerException when calling a getter
    text
    copied!<p>I have already done research on this question, and also tried to figure it out by myself but no luck. So I decided to ask it.</p> <p><b>Basic info:</b></p> <p>There are two classes. <code>FBClient</code>, and <code>State</code>. In <code>FBClient</code>, I have a static variable of it's type, <code>fbc</code>, a <code>StateManager</code> instance, which just has some methods to work with <code>State</code> stuff, some constants and two getters. In <code>State</code>, I am trying to initialize a <code>BufferedImage</code>.</p> <pre><code>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(); } } </code></pre> <p><b>More info:</b></p> <p>If I put literals in BufferedImage initialization it works.</p> <p>If I initialize two variables in the <code>State</code> class, assigning them literals and putting those variables in initialization, it works.</p> <p>If instead of assigning literals to those variables I assign them <code>FBClient.fbc.getFRAME_WIDTH()</code> and <code>FBClient.fbc.getFRAME_HEIGHT()</code>, it throws a <code>NullPointerException</code>.</p> <p>If I make a <code>System.out.println(getFRAME_WIDTH + " : " + getFRAME_HEIGHT)</code> in <code>FBClient</code> class, it prints out properly, but if I do it in <code>State</code> class (and off course adding <code>FBClient.fbc.</code> before it), it throws a <code>NullPointerException</code>.</p> <p>If I make <code>FRAME_WIDTH</code> and <code>FRAME_HEIGHT</code> constants <code>public</code>, and I try to access them from <code>State</code> class by doing <code>FBClient.fbc.FRAME_WIDTH</code> and <code>FRAME_HEIGHT</code>, it throws a <code>NullPointerException</code>.</p> <p>If I try to access the constants from <code>FBClient</code> class directly, instead of getters, it still prints out properly.</p> <p><b>Finally</b></p> <p>Thank you for taking your time, and if you need more information to work with, ask me in the comments and I'll provide it. Also, I apologise if the question is not constructed well/not explained well. If that's the case, tell me how can I improve it. And also, I apologise if this question was asked and answered once already, I may have missed it, but as I said, I did my research.</p> <p><b>Edit #1</b></p> <p>A comment suggested I print out a <code>fbc</code> value, to see if it's null. So I added this line of code to the <code>State</code> constructor: <code>if(FBClient.fbc != null) System.out.println("Not null"); else System.out.println("Null");</code> And, as suspected, it printed out null. Why is that? I clearly initialized the variable in the <code>main</code> method...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload