Entity[] not working?

Hi there!

I started today on making the building-system. But then I saw a problem I couldn’t fix (atleast with my “skills”). I thaught it would be very simple just like I did with the Strings:

private static String[] buttonName = {"Woodcutter", "Forester", "Sawmill", "Quarry", "Farm", "Mill", "Bakery", "Swine farm", "Butcher", "Fisherman"};

So I made this:

public static Entity holdingConstruction = null;    
private static Entity[] buttonConstruction = {tree, cave /*NOTE: I used 2 entities as example*/};

and in the void where the clicking code is I used this:

holdingConstruction = buttonConstruction[i];

But when I try:

holdingConstruction = buttonConstruction[i];
System.out.println(holdingConstruction + ", " + i);

It will say: null, 0 (This should then not be null but tree, atleast that is what I think)

So I knew there was something wrong with this. I searched google and looked in the docs.oracle.com and didn’t saw a thing what I was doing wrong. So my question is: How can I fix this?
If you know how I can fix this, please do NOT give me the actual code. I am new to Java and still need to learn coding. By doing it myself I will learn more then I copy things!

Already thanks!
-RoseSlayer

Did you initialize “tree” object?

public class Store {
	private static int storeLength = 10;
	private static int renderOnThisX = 10;
	private static int renderOnThisY = 10;
	public static boolean showBuildingHUD = false;
	public static boolean isHoldingConstruction = false;
	public static Entity holdingConstruction = null;
	static ConstructionTree tree;
	static ConstructionCave cave;
   private static Entity[] buttonConstruction = {tree, cave};
	private static String[] buttonName = {"Woodcutter", "Forester", "Sawmill", "Quarry", "Farm", "Mill", "Bakery", "Swine farm", "Butcher", "Fisherman"};
	public static Point mouse = new Point(0, 0);

Yes.

To initialize an object means to do this: object = new Object()… tree = new ConstructionTree() in your case. “Object object;” is just declaring an object field.

I think I tried what you said:

[EDIT]:

	static ConstructionTree tree = new ConstructionTree(Game.level,0,0,0,0);
	static ConstructionCave cave = new ConstructionCave(Game.level,0,0,0,0);					

It now works, sorry I didn’t saw the “.” in the sentence after case. Thx for helping me :smiley:

[EDIT]:
atleast it work 50% for 1 instance. now the renderfunctie and placing multiple tree’s isn’t working anymore:

			RenderConstruction.render(screen, renderOnThisX, renderOnThisY, holdingConstruction, false);
if(holdingConstruction == cave){
			cave = new ConstructionCave(Game.level, renderOnThisX, renderOnThisY, 0, 0);
			Game.level.addEntity(cave);
			
		} else if(holdingConstruction == tree){
			tree = new ConstructionTree(Game.level, renderOnThisX, renderOnThisY, 0, 0);
			Game.level.addEntity(tree);
		}
		isHoldingConstruction = false;
		holdingConstruction = null;

EDIT: Oops, didn’t see you edited your post to say you solved it :slight_smile: