Problem with finding XML files in IntelliJ even though they are there

First post here so bear with me, also tell me if im doing something wrong 🙂

I have this problem that in my IDE the application works just fine and it loads all the XML files correctly with all the data. But when I "Build artifact" to make a release the released application.jar does NOT show all of my XML data.

After alot of googling I think it has to do with where I place my XML files and folders because when I tried to recreate the error in my IDE it gave me NullPointerException to the filepath.

This application is to be used by other people so hardcoding the absolute path is not an option.

Also good to know is that I am have two functions.

–> One function for reading only one XML file located in its own package inside src.

–> Another function used to read several XML files from a seperate package inside src.

I will paste the code below aswell as a picture showing my package structure in IntelliJ IDE.

▼ Picture of folder structure here ▼

https://i.stack.imgur.com/M9xap.png

I have tried marking ItemsXML and MonsterXML as resource in project structure but no change.

▼ Reading of one XML file below ▼

public void ReadItemXMLfile(){         try{             String fileName = "src\ItemsXML\items.xml";              DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();             factory.setNamespaceAware(true);             DocumentBuilder builder = factory.newDocumentBuilder();             Document doc = builder.parse(fileName);              XPathFactory xpathfactory = XPathFactory.newInstance();             XPath xpath = xpathfactory.newXPath();              XPathExpression expr = xpath.compile("/items/item"); // LOOT ID NUMBER             Object result = expr.evaluate(doc, XPathConstants.NODESET);             NodeList nodes = (NodeList) result;              for (int i = 0; i < nodes.getLength(); i++) {                  Node testNode = nodes.item(i);                 if(testNode.getNodeType() == Node.ELEMENT_NODE){                     Element element = (Element) testNode;                     String idFromItemXml = "";                     String itemNameFromItemXml = "";                     idFromItemXml = element.getAttribute("id");                     itemNameFromItemXml = element.getAttribute("name");                     for(MonsterXML monster : monstersArrayList){                         for(MonsterLootXML loot : monster.getLootableItems()){                             if(loot.getId().equals(idFromItemXml)){                                 loot.setName(itemNameFromItemXml.substring(0, 1).toUpperCase() + itemNameFromItemXml.substring(1));                             }                         }                     }                 }             }         } catch (ParserConfigurationException parserConfigurationException) {             parserConfigurationException.printStackTrace();         } catch (IOException ioException) {             ioException.printStackTrace();         } catch (XPathExpressionException xPathExpressionException) {             xPathExpressionException.printStackTrace();         } catch (SAXException saxException) {             saxException.printStackTrace();         }     } 

▼Reading of several XML files in a folder below▼

public void ReadMonsterXMLfiles(){         try{             File dir = new File("src\\MonsterXML");             if (dir.exists() && dir.isDirectory()) {                 File [] files = dir.listFiles((d, name) -> name.endsWith(".xml"));                 if (files != null) {                     for (File file: files) {                          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();                         factory.setNamespaceAware(true); // never forget this!                         DocumentBuilder builder = factory.newDocumentBuilder();                         Document doc = builder.parse(file.getPath());                          XPathFactory xpathfactory = XPathFactory.newInstance();                         XPath xpath = xpathfactory.newXPath();                          XPathExpression expr = xpath.compile("/monster/@name |  /monster/@experience |  /monster/@manacost | /monster/health/@now");                         Object result = expr.evaluate(doc, XPathConstants.NODESET);                         NodeList nodes = (NodeList) result;                          MonsterXML monsterXML = new MonsterXML();                          monsterXML.setName(nodes.item(2).getTextContent());                         monsterXML.setHealth(nodes.item(3).getTextContent());                         monsterXML.setExperience(nodes.item(0).getTextContent());                         monsterXML.setManaToSummon(nodes.item(1).getTextContent());                         monsterXML.setName(monsterXML.getName().substring(0, 1).toUpperCase() + monsterXML.getName().substring(1));                          // MONSTER LOOT (ID) AND MONSTER LOOT (DROPCHANCE%)                         expr = xpath.compile("/monster/loot//item"); // LOOT ID NUMBER                         result = expr.evaluate(doc, XPathConstants.NODESET);                         nodes = (NodeList) result;                         MonsterLootXML monsterLootXML = null;                         for (int i = 0; i < nodes.getLength(); i++) {                             Node testNode = nodes.item(i);                             if(testNode.getNodeType() == Node.ELEMENT_NODE){                                 Element element = (Element) testNode;                                 monsterLootXML = new MonsterLootXML();                                 monsterLootXML.setId(element.getAttribute("id"));                                 monsterLootXML.setLootChance(element.getAttribute("chance"));                                 monsterLootXML.setLootChance(Calculations.correctDropChanceNumber(monsterLootXML.getLootChance()));                                  if(element.hasAttribute("countmax")){                                     monsterLootXML.setAmount(element.getAttribute("countmax"));                                 }                                 else{                                     monsterLootXML.setAmount("1");                                 }                                 monsterXML.addLootableItems(monsterLootXML);                             }                         }                         monstersArrayList.add(monsterXML);                     }                 }             }         }         catch (Exception e) {             e.printStackTrace();         }     } 

If anyone knows this well I would love to get some tutoring on discord if possible 🙂

Thanks you all!

Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.