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:
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:
<?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:
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:
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.