Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (575)
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 569 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: 23



« 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.

1  
2  
3  
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.

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

JGO Coder


Medals: 23



« 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 »

1  
2  
3  
4  
5  
6  
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.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: 23



« 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: 4
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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (35 views)
2014-10-17 03:59:02

Norakomi (25 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (26 views)
2014-10-15 16:18:58

TehJavaDev (50 views)
2014-10-14 00:39:48

TehJavaDev (50 views)
2014-10-14 00:35:47

TehJavaDev (40 views)
2014-10-14 00:32:37

BurntPizza (63 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (75 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org 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‑gaming.org
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!