user3386026
user3386026

Reputation: 41

Java boolean returning null after having been defined

After looking through multiple other questions similar, I still seem to have this problem. This seems to be the only relavent post, but it still does not answer my question. The issue I'm having is in my main function I establish a boolean, but in a function I call after that it gives me an NPE. Shown here:

public class Display extends JFrame {
private static final long serialVersionUID = 1L;

public JPanel gp = (JPanel) getGlassPane();
public Images i;
public ActualGameClass p;
public Draw d;
public Controls c;
public boolean AllLoaded = false;

public Display(){
    i = new Images();
    gp.setVisible(true);
    c = new Controls(this, d);
    c.loadControls();
    p = new ActualGameClass();
    d = new Draw(p, i);
    i.loadImages();
    System.out.println(c.ControlsLoaded);
    AllLoaded = true;
    gp.setLayout(new GridLayout(1, 1, 0, 0));
    this.setLayout(new GridLayout(1, 1, 0, 0));

    gp.add(d, p);
    this.add(i);
    p.Game();
}

then in the class ActualGameClass.java

public void Game(){
    if(Main.f.AllLoaded){
    //game
    }
}

When I run this code, it points me to the

if(Main.f.AllLoaded)

saying there is an NPE.

Because it was requested and I'm having trouble formatting in the comments: Main is just what I use to set a window in JFrame here's the relevant class

public class Main {

public static Display f = new Display();
public static int w =
        Main.f.ResChoiceW + 0;
public static int h = 
        Main.f.ResChoiceH +22; //Top Bar of GP renders as 22px
public static void main(String args[]){
    f.setSize(w, h);
    f.setResizable(false);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setTitle("Eddy's Adventures");
    f.setLocationRelativeTo(null);
    f.setAlwaysOnTop(false);
}

and the Stacktrace

Exception in thread "main" java.lang.ExceptionInInitializerError
    Caused by: java.lang.NullPointerException
        at com.fireFlower.pasterino.ActualGameClass.Game(ActualGameClass.java:24)
        at com.fireFlower.pasterino.Display.<init>(Display.java:31)
        at com.fireFlower.pasterino.Main.<clinit>(Main.java:7)

Upvotes: 2

Views: 134

Answers (2)

aliteralmind
aliteralmind

Reputation: 20163

To continue from @JustinJasmann`s answer:

  1. Eliminate the call to p.Game() in your Display constructor. Recursive calls should be avoided unless you really know what you're doing.

  2. Eliminate the AllLoaded variable entirely

  3. Change void Game() to void Game(Display disp)

  4. As the last line in your main (I think), call Game(f).

And consider naming your variables to be more indicative of what they are, and to follow naming conventions:

f         --> disp
void Game --> void startGame   //Functions should start with lowercase.
w         --> resNum  ??
h         --> resNumH

and so on.

Upvotes: 0

Justin Jasmann
Justin Jasmann

Reputation: 2353

Because you're running p.Game(); inside of the Display constructor and that references a variable that is also assigned inside the constructor of Display, you will be referencing an incompletely constructed Display object.

This should help with understanding: http://vlkan.com/blog/post/2014/02/14/java-safe-publication/

Upvotes: 4

Related Questions