Question

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.

Basic info:

There are two classes. FBClient, and State. In FBClient, I have a static variable of it's type, fbc, a StateManager instance, which just has some methods to work with State stuff, some constants and two getters. In State, I am 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();
    }
}

More info:

If I put literals in BufferedImage initialization it works.

If I initialize two variables in the State class, assigning them literals and putting those variables in initialization, it works.

If instead of assigning literals to those variables I assign them FBClient.fbc.getFRAME_WIDTH() and FBClient.fbc.getFRAME_HEIGHT(), it throws a NullPointerException.

If I make a System.out.println(getFRAME_WIDTH + " : " + getFRAME_HEIGHT) in FBClient class, it prints out properly, but if I do it in State class (and off course adding FBClient.fbc. before it), it throws a NullPointerException.

If I make FRAME_WIDTH and FRAME_HEIGHT constants public, and I try to access them from State class by doing FBClient.fbc.FRAME_WIDTH and FRAME_HEIGHT, it throws a NullPointerException.

If I try to access the constants from FBClient class directly, instead of getters, it still prints out properly.

Finally

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.

Edit #1

A comment suggested I print out a fbc value, to see if it's 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"); And, as suspected, it printed out null. Why is that? I clearly initialized the variable in the main method...

Was it helpful?

Solution 2

The reason you are having a problem is because you are trying to reference FBClient.fbc within its constructor call and the object hasn't finished its own construction. It's not immediately obvious you're doing this but if you follow the code within the constructor you are calling init() which ultimately calls to a State constructor, which in turn tries to use FBClient.fbc.getFRAME_WIDTH().

I suggest you don't call init() within the FBClient constructor and change your main method code to:

    public static void main(String[] args)
    {
        try
        {
          //First call in exception chain:
          fbc = new FBClient();
          fbc.init();
        }
        catch (Exception e)
        {
          e.printStackTrace();
          System.exit(1);
        }

    }

Hope this helps.

OTHER TIPS

You are referring to FBClient.fbc before it is assigned (actually in the constructor since fbc get assigned after the constructor finished working). To fix it add static to the final values, make the getter static and acess it with FBClient.getFRAME_HEIGHT(). You don't need non-static final variables.

I think your FBClient.fbc is null.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top