Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help with XML Parsing

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with XML Parsing

    I was working with XML parsing.Since I am learning it just now it would be good if somebody helps me to find the errors in my code

    This is the code snippet:


    I have declared all the variables correctly...

    .....
    .....
    try {
              myURL = new URL(fao_addr);
    	}
    catch (MalformedURLException e)
     {
    	System.out.println(e);
    	e.printStackTrace();
    }
     
    	try {
    			feed = xmlReader.read(fao_addr);
              	} catch (DocumentException e) {
    			System.out.println(e);
    			e.printStackTrace();
    		}
     
    	String xpathExpression = "/Data/FAO";
            nodes = feed.selectNodes(xpathExpression);
     
     
            String xpathExpression2 = "/Data/FAO/region/hasMember";
            List<? extends Node> subnodes = feed.selectNodes(xpathExpression2);
     
            System.out.println("The FAO Members by region in "+language+ " language are : ");
     
     
            for (Node node : nodes) {
     
            	Node region = node.selectSingleNode("region");
            	System.out.println(region.getText());
            	for(Node subnode : subnodes)
            	{
            		String acc_variable="nameShort"+language;
            		Node short_name = subnode.selectSingleNode(acc_variable);
            		System.out.println(short_name.getText()+"\n");                      //Error Here..
            	}
     
            }

    This is the XML file I am trying to access

    http://www.fao.org/countryprofiles/g...FAOByRegion/EN

    This is the error I get in the mentioned line

    Exception in thread "main" java.lang.NullPointerException


    Thanks in advance


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Help with XML Parsing

    Use code tags for your code, quote or code tags for your error messages. Post full stack traces of any exceptions.

Similar Threads

  1. Parsing XML
    By jrookie in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 17th, 2011, 10:04 AM
  2. Question regarding XML Parsing
    By newbie in forum Java Theory & Questions
    Replies: 2
    Last Post: February 24th, 2011, 06:03 AM
  3. Java XML Parsing
    By nimilc2002 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 9th, 2011, 11:00 AM
  4. Parsing urls
    By Riddhi Sharma in forum Java Theory & Questions
    Replies: 2
    Last Post: January 25th, 2011, 10:06 AM
  5. Parsing CDATA
    By Sai in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 9th, 2010, 12:33 AM