1

I am trying to keep record of a persons name, score etc.

I get massive errors when I try to compile? Maybe I am just missing something but it looks right to me, please help. Thank you very much!

        private void scoreButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            

    try { 
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new File("scores.xml"));
        doc.getDocumentElement().normalize();
        System.out.println("Root element of the doc is "+ doc.getDocumentElement().getNodeName());
        NodeList listOfPlayers = doc.getElementsByTagName("player");
        int totalPersons = listOfPlayers.getLength();
        System.out.println("Total number of people: "+ totalPersons);



    for (int s = 0; s<listOfPlayers.getLength(); s++) {
            Node firstPersonNode = listOfPlayers.item(s);
          if (firstPersonNode.getNodeType() == Node.ELEMENT_NODE) {
                Element firstPersonElement = (Element) firstPersonNode;
                NodeList firstNameList = firstPersonElement.getElementsByTagName("first");
                Element firstNameElement = (Element) firstNameList.item(0);
                NodeList textFNList = firstNameElement.getChildNodes();
                System.out.println("First Name: " + ((Node)textFNList.item(0)).getNodeValue().trim());
                NodeList lastNameList = firstPersonElement.getElementsByTagName("last");
                Element lastNameElement = (Element)lastNameList.item(0);
                NodeList textLNList = lastNameElement.getChildNodes();
                System.out.println("Money: " + ((Node)textLNList.item(0)).getNodeValue().trim());
                NodeList ageList = firstPersonElement.getElementsByTagName("age");
                Element ageElement = (Element)ageList.item(0);
                NodeList textAgeList = ageElement.getChildNodes();
                System.out.println("Age: " + ((Node)textAgeList.item(0)).getNodeValue().trim());

            }
        } catch(SAXParseException err) {

    }
}

Here is the scores.xml file:

    <scores>

<player>
    <name> Sam Moeza </name>
    <money> 100 </money>
    <age> 22 </age>
</player>

<player>
    <name> Larry Hoover </name>
    <money> 100000 </money>
    <age> 55 </age>
</player>

    </scores>

Here is the error:

    Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - unreported exception javax.xml.parsers.ParserConfigurationException; must be caught or declared to be thrown
    at casinoroulette.CasinoRouletteView.scoreButtonActionPerformed(CasinoRouletteView.java:327)
    at casinoroulette.CasinoRouletteView.access$1200(CasinoRouletteView.java:24)
    at casinoroulette.CasinoRouletteView$8.actionPerformed(CasinoRouletteView.java:173)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at    java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Traplord
  • 101
  • 6

1 Answers1

2

Let's start with the obvious...

You're missing a closing } after the if or for...

try { 
    //...
    for (int s = 0; s<listOfPlayers.getLength(); s++) {
        Node firstPersonNode = listOfPlayers.item(s);
        if (firstPersonNode.getNodeType() == Node.ELEMENT_NODE) {
            //...
        //.. nothing here?
    }
} catch(SAXParseException err) {

}

Then move onto that fact that you'll need expand your try-catch to deal with javax.xml.parsers.ParserConfigurationException and java.io.IOException

For example...

try { 
    //...
    for (int s = 0; s<listOfPlayers.getLength(); s++) {
        Node firstPersonNode = listOfPlayers.item(s);
        if (firstPersonNode.getNodeType() == Node.ELEMENT_NODE) {
            //...
        } // Fix me
    }
} catch (ParserConfigurationException | SAXException | IOException ex) {
    // Don't forget to do something with your exception, like
    // log it or show and error message dialog
    ex.printStackTrace();
}

If you're using Java 7+ you can use the multi-catch demonstrated above, otherwise, you'll need to catch each one individually...

You might like to take some time reading through the Exceptions trail for more information

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366