Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (603)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (651)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Instances problem[Solved]  (Read 875 times)
0 Members and 1 Guest are viewing this topic.
Offline SonicPixelation

Senior Newbie

« Posted 2013-03-05 15:51:48 »

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.
Offline actual

JGO Coder

Medals: 24

« Reply #1 - Posted 2013-03-05 16:52:39 »

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.
Offline SonicPixelation

Senior Newbie

« Reply #2 - Posted 2013-03-05 17:13:35 »

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

   at org.TEKGames.World.TiledWorld.LoadWorldFromImage(
   at com.test.Game.init(
   at org.newdawn.slick.AppGameContainer.setup(
   at org.newdawn.slick.AppGameContainer.start(
   at com.test.Game.main(

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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline actual

JGO Coder

Medals: 24

« Reply #3 - Posted 2013-03-05 17:40:27 »

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).
Offline SonicPixelation

Senior Newbie

« Reply #4 - Posted 2013-03-05 17:47:19 »

                     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.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();
Offline SonicPixelation

Senior Newbie

« Reply #5 - Posted 2013-03-05 17:52:24 »

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
Offline actual

JGO Coder

Medals: 24

« Reply #6 - Posted 2013-03-05 17:53:52 »

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.
Offline StonePickaxes

JGO Coder

Medals: 5
Projects: 2

Nathan Kramber

« Reply #7 - Posted 2013-03-05 17:56:00 »

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

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

Check out my website!
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

SHC (11 views)
2015-08-01 03:58:20

Jesse (17 views)
2015-07-29 04:35:27

Riven (38 views)
2015-07-27 16:38:00

Riven (19 views)
2015-07-27 15:35:20

Riven (22 views)
2015-07-27 12:26:13

Riven (12 views)
2015-07-27 12:23:39

BurntPizza (32 views)
2015-07-25 00:14:37

BurntPizza (43 views)
2015-07-24 22:06:39

BurntPizza (25 views)
2015-07-24 06:06:53

NoxInc (31 views)
2015-07-22 22:16:53
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!