Instances problem[Solved]

OK, I was working on getting a simple tiled map system up and running to be used as a library, to help make the prototyping process easier. What I do is I have a register that contains integer array that hold the hexadecimal and a Tile array that just holds the class of the tile.

in the world loading method it take from the data from Tile register and looks for a color that matches. and that brings me to my problem how can I make a new instance of the class that is in the Tile class array; I tried calling newInstance() on it but it just threw an exception and crashed the application.

any help would be appreciated.

If I understand correctly, you have a bunch of Tile class objects (WoodTile.class, RoadTile.class, etc) and you want to be able to then create an instance of each while looping. From this Stackoverflow question it looks like you first need to get the constructor before you create the instance.


Class<?> tileClass = tiles[5];
Constructor<?> constructor = tileClass.getConstructor();
Tile tile = constructor.newInstance();

This assumes that all of your Tile class objects inherit from a Tile base class.

yeah that was the idea I had in mind but it throws a lot of exceptions.


java.lang.NullPointerException
	at org.TEKGames.World.TiledWorld.LoadWorldFromImage(TiledWorld.java:82)
	at com.test.Game.init(Game.java:21)
	at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:433)
	at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:357)
	at com.test.Game.main(Game.java:40)

It spams that in the console
but for some reason still renders the world

btw I am also using lwjgl and slick2d

and it also says that I need to cast
constructor.newInstance() to tile

What is on line 82? Do you know what specifically what value is null?

It’s worth creating a small program that just takes a Tile class and creates an instance from it. Isolating it that way will make it easier to see if there really is something wrong with creating the class, or if it is something else in your code (like your Tile list not being populated fully).


try{
							Constructor con = TR.tileClass[i].getConstructor();
							if(con.newInstance() instanceof Tile){
								 tile = (Tile)con.newInstance();
							}
						}catch(Exception e){e.printStackTrace();}

this is the code for what you suggested

Constructor con = TR.tileClass[i].getConstructor();

and thats is what is on line 82

but even with the errors it doesn’t seem to effect anything so i could do a cheat fix and just remove e.printStackTrace();

never mind fixed it I just need to look it see if the tilelist had nulls in it and then just throw those away, my fault , but thanks anyway

According to the API docs getConstructor() shouldn’t ever return null. So I am guessing that some of the elements in your tileClass array are null. So it complains when it hits the blanks, but renders the others.

Just before those lines, loop through tileClass and have it print the number of elements that are null. If it’s > 0, then that explains it.

ALWAYS check what exception was thrown first, especially if it’s a null pointer. Those are very easy to fix.