Vinod CG
Vinod CG

Reputation: 1061

parsing XML files using java- null pointer exception

Hi everyone i'm trying to parse xml files using java using code below..

try{

        DocumentBuilderFactory docFactory=DocumentBuilderFactory.newInstance();
        DocumentBuilder docBulider=docFactory.newDocumentBuilder();
        Document config_doc=docBulider.parse("config/appconfig.xml");
        config_doc.getDocumentElement().normalize();
       Node n =config_doc.getDocumentElement();
      NodeList list= n.getChildNodes();
      for(int i=0;i<list.getLength();i++){
          System.out.print(list.item(i));
        if(list.item(i).getNodeName().equalsIgnoreCase("version-name")){
         name=list.item(i).getNodeValue();
        }

      }

        }
        catch (Exception e){e.printStackTrace();}

My file layout is like this

File Layout

I keep getting file not found exception. I also used

getClass().getResouce("config/appconfig.xml").toExternalForm()

and i tried to read as stream too.

Thank you :)

Upvotes: 0

Views: 197

Answers (3)

Love Bisaria
Love Bisaria

Reputation: 167

You can give the absolute path as :

Document config_doc=docBulider.parse(new File("$Absolute Path of the file"));

Upvotes: 0

jorgeu
jorgeu

Reputation: 689

this one seems good

getClass().getResouce("config/appconfig.xml").toExternalForm()

except that it is main/config/appconfig.xml

Upvotes: 1

PiersyP
PiersyP

Reputation: 5233

Try using

Sytsem.out.println(System.getProperty("user.dir"));

This will print out your program's current working directory, then you can figure out the relative path to your appconfig.xml file.

Good luck

Piers

Upvotes: 1

Related Questions