2

I'm making a 2D game in Java. I have a Sprite class and a Tile class. First I declared some static Sprite-objects in the Sprite-class

public class Sprite {   
public static Sprite grass = new Sprite(0, 0, Spritesheet.testTiles);
...

And then I declared static Tile-objects in the Tile-class, and passed the static Sprite-objects as arguments, like this:

public class Tile {
public static Tile grass = new Tile("grass", Sprite.grass);
...

However for some reason the Sprite I'm passing is null. Any ideas why?

Whenever I'm using the grass-object anywhere else I have no problems.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
YXalr
  • 19
  • 3
  • 1
    probably because you create the static Tile before the static Sprite. You might have cyclic "dependencies" between your static instances. – luk2302 May 20 '17 at 11:18
  • [It works for me](http://ideone.com/mE6Sqg) – BackSlash May 20 '17 at 11:19
  • Ok, the problem was with cyclic dependencies. Thank you! – YXalr May 20 '17 at 11:23
  • When you found a solution for your problem when you write an answer post and don't edit your question. And no "it was a cyclic dependency problem" is not a good answer. Show the code with that dependency and how you've fixed it. – Tom May 20 '17 at 11:26

3 Answers3

2

That is totally expected, static initialization happens on different events which none of them occurs before your assign, so the static attribute will be null anyway.

See this post for more details on the events

Explanation of how classloader loads static variables

Community
  • 1
  • 1
Amer Qarabsa
  • 6,412
  • 3
  • 20
  • 43
0

The Tile.grass field is being initialized earlier than Sprite.grass. The question bellow provides some insight about it.

Java : in what order are static final fields initialized?

A solution for this could be turning Sprite.grass and Tile.grass into singletons, and accessing them by a method:

class Sprite {   
    private static Sprite grass;

    public static Sprite getGrassInstance(){
       if(grass == null){
           grass = new Sprite(0, 0, Spritesheet.testTiles);
       }

       return grass;
    }

}

But be aware of cyclical dependencies between them to avoid an infinite loop.

Community
  • 1
  • 1
0

It turned out to be a problem with cyclic dependencies. I had a static variable (TILE_SIZE) in the Tile-class which I used elsewhere in the code, including the Sprite-class. I moved the variable in the Sprite-class and removed the dependency to Tile-class.

original:

import ge.game.Tile;

public class Sprite {   
public static Sprite grass = new Sprite(0, 0, Spritesheet.testTiles);
...

import ge.graphics.Sprite;

public class Tile {
public static Tile grass = new Tile("grass", Sprite.grass);
public static int TILE_SIZE = 16;
...

new:

public class Sprite {   
public static Sprite grass = new Sprite(0, 0, Spritesheet.testTiles);
public static int SPRITE_SIZE = 16;
...

import ge.graphics.Sprite;

public class Tile {
public static Tile grass = new Tile("grass", Sprite.grass);
...

Thank you!

YXalr
  • 19
  • 3