Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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  
  Really strange problem  (Read 2000 times)
0 Members and 1 Guest are viewing this topic.
Offline marcuiulian13

Senior Member


Medals: 5
Exp: 3 years



« Posted 2012-07-10 07:41:47 »

Hello everyone!
I'm still working at my game : http://sgamed.blogspot.ro

I'm trying to load from a XML file locations of some SpriteSheets.

Here is the code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
         xmlLoader.loadXML("res/xml/armorsXML.xml");
         xmlLoader.searchByTag("armor");
         for(int i = 0; i < xmlLoader.getNodeList().getLength(); i++) {
            Node nNode = xmlLoader.getNodeList().item(i);
            if(nNode.getNodeType() == Node.ELEMENT_NODE) {
               Element eElement = (Element) nNode;
               String armorType = xmlLoader.getTagValue("armortype", eElement);
               String aLoc;
               
               switch(armorType) {
               case "head": aLoc = xmlLoader.getTagValue("armorSheet", eElement);
                        headSheet[i] = new SpriteSheet(aLoc, 32, 48); break;
               case "chest": aLoc = xmlLoader.getTagValue("armorSheet", eElement);
                        chestSheet[i] = new SpriteSheet(aLoc, 32, 48); break;
               case "legs": aLoc = xmlLoader.getTagValue("armorSheet", eElement);
                        legsSheet[i] = new SpriteSheet(aLoc, 32, 48); break;
               case "boots": aLoc = xmlLoader.getTagValue("armorSheet", eElement);
                        bootsSheet[i] = new SpriteSheet(aLoc, 32, 48); break;
               case "hands": aLoc = xmlLoader.getTagValue("armorSheet", eElement);
                        handsSheet[i] = new SpriteSheet(aLoc, 32, 48); break;
               }
            }
         }


And this is my XML:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
<?xml version="1.0"?>
<armors>
   <armor>
      <id>0</id>
      <armortype>head</armortype>
      <name>Head</name>
      <armor>20</armor>
      <armorSheet>res/images/armor/head0.png</armorSheet>
   </armor>
   <armor>
      <id>1</id>
      <armortype>chest</armortype>
      <name>Chest</name>
      <armor>20</armor>
      <armorSheet>res/images/armor/chest0.png</armorSheet>
   </armor>
   <armor>
      <id>2</id>
      <armortype>legs</armortype>
      <name>Legs</name>
      <armor>20</armor>
      <armorSheet>res/images/armor/legs0.png</armorSheet>
   </armor>
   <armor>
      <id>3</id>
      <armortype>boots</armortype>
      <name>Boots</name>
      <armor>20</armor>
      <armorSheet>res/images/armor/boots0.png</armorSheet>
   </armor>
   <armor>
      <id>4</id>
      <armortype>hands</armortype>
      <name>Hands</name>
      <armor>20</armor>
      <armorSheet>res/images/armor/hands0.png</armorSheet>
   </armor>
</armors>


And this is the error i get:
1  
2  
3  
4  
5  
6  
7  
8  
Exception in thread "main" java.lang.NullPointerException
   at com.game.util.XMLLoader.getTagValue(XMLLoader.java:32) //Line 30 in posted code
  at com.game.util.ResourceLoader.loadImages(ResourceLoader.java:57) //Line 7 in posted code
  at com.game.ClientLauncher.initStatesList(ClientLauncher.java:42)
   at org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:164)
   at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:390)
   at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:314)
   at com.game.ClientLauncher.main(ClientLauncher.java:38)


Another thing i found is that if i try to print the "xmlLoader.getNodeList().getLength()", i get 10, instead of 5.

Here is the code of my XMLLoader:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

public class XMLLoader {
   private NodeList nodeList;
   private Document doc;

   public void loadXML(String location) {
      try {
         File fXmlFile = new File(location);
         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
         DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
         doc = dBuilder.parse(fXmlFile);
         doc.getDocumentElement().normalize();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   public void searchByTag(String tag) {
      nodeList = doc.getElementsByTagName(tag);
   }

   public String getTagValue(String sTag, Element eElement) {
      NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
      Node nValue = (Node) nlList.item(0);
      return nValue.getNodeValue();
   }

   public NodeList getNodeList() { return nodeList; }
}


Thanks in advance. Cheesy

Getting a project done is by far the most hard thing in game development.
Offline 65K
« Reply #1 - Posted 2012-07-10 08:20:07 »

Start a debugger to see exactly which object is null at the given line.
Thus, you can also examine the actual read xml structure at runtime.

Offline marcuiulian13

Senior Member


Medals: 5
Exp: 3 years



« Reply #2 - Posted 2012-07-10 10:34:22 »

I am using Slick2D and i really don't know how to see the needed variable in that tree...

Getting a project done is by far the most hard thing in game development.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 65K
« Reply #3 - Posted 2012-07-10 10:38:33 »

Just set a breakpoint at line 30 of your loader class and see which object is null.
Or set a general NullpointException-Breakpoint (possible in Eclipse for example), run, and wait until it stops.

Offline atombrot

Senior Member


Medals: 9
Projects: 1



« Reply #4 - Posted 2012-07-10 11:57:34 »

You could put a try catch in the getNodeValue method, that encloses the code... If it fails print out the parameters that lead to to the problem...

Something like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public String getTagValue(String sTag, Element eElement) {
      try{
          NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
          Node nValue = (Node) nlList.item(0);
          return nValue.getNodeValue();
      }
     catch(Exception ex)
     {
        System.out.println(string.Format("getTagValue failed for sTag '%s'%s", sTag, eElement == null ? " (eElement is null)" : ""));
        ex.printStackTrace();
        throw ex;
     }
   }


I'm not sure if the code is valid, as I have no running eclipse here Wink
Offline marcuiulian13

Senior Member


Medals: 5
Exp: 3 years



« Reply #5 - Posted 2012-07-10 15:22:47 »

I tried your solution, but it print:
1  
2  
3  
4  
5  
6  
7  
8  
9  
getTagValue failed for sTag 'armortype'
java.lang.NullPointerException
   at com.game.util.XMLLoader.getTagValue(XMLLoader.java:33)
   at com.game.util.ResourceLoader.loadImages(ResourceLoader.java:56)
   at com.game.ClientLauncher.initStatesList(ClientLauncher.java:40)
   at org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:164)
   at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:390)
   at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:314)
   at com.game.ClientLauncher.main(ClientLauncher.java:36)


So, it doesn't print that element is null.. Any solution?

Getting a project done is by far the most hard thing in game development.
Offline atombrot

Senior Member


Medals: 9
Projects: 1



« Reply #6 - Posted 2012-07-10 15:28:10 »

This means that the parameter you passed is not null... you could add some more checks:
1  
2  
3  
4  
5  
6  
7  
...
NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
if(nlList == null) throw new Exception("nlList is null");
Node nValue = (Node) nlList.item(0);
if(nValue == null) throw new Exception("nValue is null");
return nValue.getNodeValue();
...


like this you can isolate the problem. You still have to find out why the object is null, but you should be able to find out which one it was.

[edit]
also this line could be problematic:
1  
NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();


You access the item directly without checking if it is available... I don't have eclipse ready here, but i think you should change that part into something like that
1  
2  
3  
NodeList list = eElement.getElementsByTagName(sTag);
if(list.getLength() <= 0) throw new Exception("No node found");              
NodeList nlList = list.item(0).getChildNodes();

[/edit]
Offline marcuiulian13

Senior Member


Medals: 5
Exp: 3 years



« Reply #7 - Posted 2012-07-10 15:29:59 »

Yeah, but the problem is that for another file, this works great.

This is wierd...

EDIT: This is the code for the other file:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
xmlLoader.loadXML("res/xml/weaponsXML.xml");
         xmlLoader.searchByTag("weapon");
         for(int i = 0; i < xmlLoader.getNodeList().getLength(); i++) {
            Node nNode = xmlLoader.getNodeList().item(i);
            if(nNode.getNodeType() == Node.ELEMENT_NODE) {
               Element eElement = (Element) nNode;
               String wLoc = xmlLoader.getTagValue("weaponSheet", eElement);
               String aLoc = xmlLoader.getTagValue("attackSheet", eElement);
               weaponsSheet[i][0] = new SpriteSheet(wLoc, 48, 48);
               weaponsSheet[i][1] = new SpriteSheet(aLoc, 48, 48);
            }
         }


And this is the other file:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
<?xml version="1.0"?>
<weapons>
   <weapon>
      <id>0</id>
      <name>Sword</name>
      <damage>20</damage>
      <weaponSheet>res/images/weapons/swordSheet.png</weaponSheet>
      <attackSheet>res/images/weapons/swordAttackSheet.png</attackSheet>
   </weapon>
</weapons>

Getting a project done is by far the most hard thing in game development.
Offline marcuiulian13

Senior Member


Medals: 5
Exp: 3 years



« Reply #8 - Posted 2012-07-10 15:39:01 »

The problem isn't for weapons, it's with the armor. The weapons work as they should.

Getting a project done is by far the most hard thing in game development.
Offline atombrot

Senior Member


Medals: 9
Projects: 1



« Reply #9 - Posted 2012-07-10 15:45:50 »

Sorry, removed my post as I noticed its irelevance XD

Currently I can't see whats causing the problem. You have to find out, which call to getTagValue causes to fail and than analyze that case... Do you work with eclipse? Then you can set a breakpoint, when you right click on the left hand side of the code window. When you start the programm with "Debug As" (or by pressin F11), the programm is started in debug mode.

Its best you set the break point in the loadImages method (looking at the call stack, it seems that that is the root method described in your first post) and then step over every call with F6. If you find the call that causes the problem, you can debug again and then step into that method by pressing F5 instead of F6. Then you should be able to check the variables...

Or improve error handling and validation (see edit in my last post).
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline marcuiulian13

Senior Member


Medals: 5
Exp: 3 years



« Reply #10 - Posted 2012-07-10 15:50:21 »

OH MY GOD!
I solved the problem. Emo
This was so noob level.

If you look in my armors XML, you can see that in my ARMOR ELEMENTS I made an ARMOR PROPRETY. This is why I was getting length = 10 instead of 5.
I can't belive I lost so much time for this thing. Thanks to everyone. Grin

Getting a project done is by far the most hard thing in game development.
Offline atombrot

Senior Member


Medals: 9
Projects: 1



« Reply #11 - Posted 2012-07-10 15:59:15 »

You're welcome...

Ahaha.. I should have spotted that one XD

Btw. ever thought of serializing and deserializing objects directly with a framework? It can save you alot of headaches... I'm using xstream, but you could be working also with another library like jaxb. When you set it up correctly you can generate human readable (and editable) xml, but do not have to care about the serialization itself.
Offline atombrot

Senior Member


Medals: 9
Projects: 1



« Reply #12 - Posted 2012-07-10 16:33:54 »

I made a little example, how you could handle serialization automatically with xstream Wink Basically you have a bit more code (you need to set aliases for classes, register classes in xstream etc.) but it is really worth the time, as you do not have fight against those manual serialization issues.

I use it in my project and all xml files consumed by my game are generated from the code... I have a model generator class, that (programmatically) creates all the game xml files. If I change something in the class it could break the model, so you have to fix that. When your model is changed, you can run the model generator and all game xml files are updated. Like this I don't have to care about the version of the xml file, as it always should be the newest version, when generation has been done.

You can find xstream here ( http://xstream.codehaus.org/ ) and I can really encourage you to do the two minute tutorial...

Code and xml output of my example => http://pastebin.com/JKyxRG6F

Short explanation about the example: The main method is in the XSTest class... The main method first generates a model in code and then saves it to a file... In a second step, this file is loaded and deserialized by xstream and the items in the store are printed out.   The console output from the test program is:
1  
2  
3  
4  
5  
6  
Generated xml 'c:\temp\items.xml':
[...]
Weapon (1): Short sword, type: swords, attack sheet: data/sprites/swordattack.png
Weapon (2): Bastard sword, type: swords, attack sheet: data/sprites/swordattack.png
Weapon (3): Long sword, type: swords, attack sheet: data/sprites/swordattack.png
Armor (4): Studded leather armor, type: Chest, armor value: 20
Offline marcuiulian13

Senior Member


Medals: 5
Exp: 3 years



« Reply #13 - Posted 2012-07-10 16:49:49 »

I don't know.. I'm a little constrained about using many external libraries.. As i said before, i am also using Slick2D.
In my vision, using many libraries => occupying memory. If I'm wrong, please, tell me.

Getting a project done is by far the most hard thing in game development.
Offline atombrot

Senior Member


Medals: 9
Projects: 1



« Reply #14 - Posted 2012-07-10 16:56:24 »

Yeah, I understand that and thats also something that concerns me. but serialization is key feature, that I use very strongly in my projects and I just don't have time to reinvent the wheel, when there is a suitable, well tested solution ready to use... i checked the jars... xstream and xppp (xstream uses the xppp library) are around 550 kb.

but you could still use JAXB, which should be part of the java default library (i don't know how this is really called... im quite new to java and still not used to the terminology)... you do not need any external libraries for jaxb...
Offline sproingie

JGO Kernel


Medals: 202



« Reply #15 - Posted 2012-07-10 17:21:45 »

A library is not automatically "lighter" just because it comes bundled with the JDK.  JAXB uses Xalan, which is a big heavy DOM implementation, so going with it just because it comes bundled with the JDK is penny-wise pound-foolish.
Offline marcuiulian13

Senior Member


Medals: 5
Exp: 3 years



« Reply #16 - Posted 2012-07-10 17:47:13 »

Can anyone point me to something that explains what is the serialization? Because I'm in mist with this subject.

Thanks!

Getting a project done is by far the most hard thing in game development.
Offline atombrot

Senior Member


Medals: 9
Projects: 1



« Reply #17 - Posted 2012-07-10 17:56:52 »

Sometimes there is the need to persist an object for later use (a save game would be a nice example). The process ,that takes the java object and creates a representation of it (may be xml ,pure bytes or even a custom text format) is called serialization. The opposite is, when you have a representation ,that should be loaded as object. This is called deserialization.

Basically ,what you do is deserializing by hand.
Offline marcuiulian13

Senior Member


Medals: 5
Exp: 3 years



« Reply #18 - Posted 2012-07-10 18:02:31 »

Ok, thank you! I should search on Google some good methods for serialization, right?

Getting a project done is by far the most hard thing in game development.
Offline atombrot

Senior Member


Medals: 9
Projects: 1



« Reply #19 - Posted 2012-07-10 18:04:42 »

A library is not automatically "lighter" just because it comes bundled with the JDK.  JAXB uses Xalan, which is a big heavy DOM implementation, so going with it just because it comes bundled with the JDK is penny-wise pound-foolish.

Yeah ,you are right. Sorry, I mixed up some things... I was thinking of the package size (the distributable game) and not of the memory usage of the library...
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.

Riven (12 views)
2014-07-29 18:09:19

Riven (10 views)
2014-07-29 18:08:52

Dwinin (9 views)
2014-07-29 10:59:34

E.R. Fleming (26 views)
2014-07-29 03:07:13

E.R. Fleming (10 views)
2014-07-29 03:06:25

pw (40 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (27 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!