0

I'm trying to parse an XML file of employees, and with the data in the XML I want to create an array list of all of the employees, but when I run the program and try to parse the XML file, it gives me a Null Point Exception. This is my XML file:

<?xml version="1.0"?>
<employees>
    <employee>
        <name>Roberta Robertson</name>
        <manager>false</manager>
    </employee>
    <employee>
        <name>Taylor Meyers</name>
        <manager>true</manager>
    </employee>
    <employee>
        <name>John Mayer</name>
        <manager>true</manager>
    </employee>
</employees>

This is my code for parsing the XML file. I call the method when I press a certain button in my GUI.

public class employeesParser {
private DocumentBuilder builder;
private XPath path;

private void employeesParser()
        throws ParserConfigurationException
{

    // create the documentbuiler and path
    DocumentBuilderFactory dbfactory 
                = DocumentBuilderFactory.newInstance();
    builder = dbfactory.newDocumentBuilder();
    XPathFactory xpfactory = XPathFactory.newInstance();
    path = xpfactory.newXPath();
}

// read the file
public ArrayList<Employee> loadEmployees(String fileName)
    throws SAXException, IOException, XPathExpressionException
{
    //parsing the file

    File f = new File(fileName);
    Document doc = builder.parse(f);

    //reading the file, extracting the employees, add employees to ArrayList
    int employeeCount = Integer.parseInt(path.evaluate(
        "count(/employees/employee)", doc));

    for (int i = 1; i <= employeeCount; i++)
    {
        String employeeName = path.evaluate(
        "/employees/employee[" + i + "]/name", doc);
        boolean managerStatus = Boolean.parseBoolean(path.evaluate(
                "employees/employee[" + i + "]/manager", doc));
        Employee employee = new Employee(employeeName, managerStatus);
        employeeList.add(employee);
    }

    return employeeList;

    }
}

This is where I call the method:

private void addEmployeeXMLActionPerformed(java.awt.event.ActionEvent evt) {                                               
    // TODO add your handling code here:
    try {
        employeesParser parser = new employeesParser();
        employeeList = parser.loadEmployees("employees.xml");
    }
    catch (SAXException|IOException|XPathExpressionException e) {
    }
}            

According to the exception message, the error occurs at this line of code:

    Document doc = builder.parse(f);

which is found in my loadEmployees() method. Can anyone tell me what I'm doing wrong? I've used code very similar to this before and it has worked. Thanks a lot!

TikiMan
  • 11
  • 3
  • 1. Use your debugger. It's your friend. Q: What IDE are you using? Eclipse? Netbeans? Other? 2. In the debugger, verify "f" isn't null when you call `Document doc = builder.parse(f);`. 3. Post back what you find. My suspicion is that "employees.xml" just doesn't happen to be in the right place. But please let us know your IDE, and please familiarize yourself with your IDE's debugger (if you haven't already done so). – paulsm4 Aug 03 '15 at 05:29
  • Please make sure your file is loading in java.io.File and try to debug your program. – Noman ali abbasi Aug 03 '15 at 05:31
  • Could you post the complete Stacktrace? – slartidan Aug 03 '15 at 05:33

1 Answers1

0

In your code, you don't have constructor defined for employeesParser. The initialization of DocumentBuilder and DocumentBuilderFactory are in a private method called 'employeesParser'. Either you move this initialization code into constructor or call this employeesParser method at the beginning of loadEmployees() method.

kswaughs
  • 2,967
  • 1
  • 17
  • 21
  • Thanks alot! I meant for that method to be my constructor but I didn't realize that it was private and void, so I changed it to public employeesParser() and it seems to work now. Thanks again! – TikiMan Aug 03 '15 at 06:48