Java - Null Pointer Exception while parsing XML file -


this question has answer here:

i'm trying parse xml file of employees, , data in xml want create array list of of employees, when run program , try parse xml file, gives me null point exception. 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 code parsing xml file. call method when press button in gui.

public class employeesparser { private documentbuilder builder; private xpath path;  private void employeesparser()         throws parserconfigurationexception {      // create documentbuiler , path     documentbuilderfactory dbfactory                  = documentbuilderfactory.newinstance();     builder = dbfactory.newdocumentbuilder();     xpathfactory xpfactory = xpathfactory.newinstance();     path = xpfactory.newxpath(); }  // read file public arraylist<employee> loademployees(string filename)     throws saxexception, ioexception, xpathexpressionexception {     //parsing file      file f = new file(filename);     document doc = builder.parse(f);      //reading file, extracting employees, add employees arraylist     int employeecount = integer.parseint(path.evaluate(         "count(/employees/employee)", doc));      (int = 1; <= employeecount; i++)     {         string employeename = path.evaluate(         "/employees/employee[" + + "]/name", doc);         boolean managerstatus = boolean.parseboolean(path.evaluate(                 "employees/employee[" + + "]/manager", doc));         employee employee = new employee(employeename, managerstatus);         employeelist.add(employee);     }      return employeelist;      } } 

this call method:

private void addemployeexmlactionperformed(java.awt.event.actionevent evt) {                                                    // todo add handling code here:     try {         employeesparser parser = new employeesparser();         employeelist = parser.loademployees("employees.xml");     }     catch (saxexception|ioexception|xpathexpressionexception e) {     } }             

according exception message, error occurs @ line of code:

    document doc = builder.parse(f); 

which found in loademployees() method. can tell me i'm doing wrong? i've used code similar before , has worked. lot!

in code, don't have constructor defined employeesparser. initialization of documentbuilder , documentbuilderfactory in private method called 'employeesparser'. either move initialization code constructor or call employeesparser method @ beginning of loademployees() method.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -