dcsimg
Help ! i can't execute this program properly using jdom
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   lily_wong
Posted On:   Saturday, October 13, 2001 05:39 PM

I'm trying to extract the data in cutomer.xml and item(x.xml which describes the requiremnt in the program. I keep geeting error like this : java.lang.NullPointerException at Order.main import java.io.*; import org.jdom.*; import org.jdom.input.*; import org.jdom.output.*; public class Order { public static void main(String[] args) { try { SAXBuilder builder = new SAXBuilder(); Document customerdoc = builder.build("customer.xml"); Element customer = customerdoc.getRootElement(); Element fname = (Element) customer.getChild("name").getChild("first").clone();    More>>

I'm trying to extract the data in cutomer.xml and item(x.xml which describes the requiremnt in the program. I keep geeting error like this :


java.lang.NullPointerException
at Order.main

			


import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;


public class Order {
public static void main(String[] args) {
try {
SAXBuilder builder = new SAXBuilder();

Document customerdoc = builder.build("customer.xml");

Element customer = customerdoc.getRootElement();

Element fname = (Element)
customer.getChild("name").getChild("first").clone();

Element lname = (Element)
customer.getChild("name").getChild("last").clone();

Element address = (Element)
customer.getChild("address").getChild("city").clone();

Element city = (Element)customer.getChild("city").clone();

Element state = (Element)customer.getChild("state").clone();

Element zipcode = (Element)customer.getChild("zipcode").clone();


Element order = new Element("order");

order.addContent(fname);

order.addContent(lname);

order.addContent(address);

order.addContent(city);

order.addContent(state);

order.addContent(zipcode);


Document orderdoc = new Document(order);

orderdoc.setRootElement(order);


Document itemdoc;
String instock;
Element item;
for (int x=0; x
itemdoc = builder.build("item" + args[x] + ".xml");
instock =
itemdoc.getRootElement().getChild("id").getAttributeValue("instock");
if (instock.equals("yes")) {
item = new Element("item");
item.addContent( (Element)
itemdoc.getRootElement().getChild("name").clone() );
item.addContent( (Element)
itemdoc.getRootElement().getChild("price").clone() );
item.addContent( (Element)
itemdoc.getRootElement().getChild("id").clone() );
order.addContent(item);
}
}

XMLOutputter out = new XMLOutputter(" ", true);
out.setTextNormalize(false);
out.output(orderdoc, System.out);
}
catch (Exception e) {
e.printStackTrace();
}
}
}


I executed the program like this :
java Order customer.xml item1.xml item2.xml


I'm also using jdom ver 7 and xerces that accopanies with it. Please help.
Thank you.    <<Less

Re: Help ! i can't execute this program properly using jdom

Posted By:   Bozidar_Dangubic  
Posted On:   Wednesday, October 17, 2001 05:58 AM

any of the calls like
customer.getChild("name").getChild("first").clone();
can produce NullPointerException since any customer that has no children will not have these tags. customer.getChild("name") can be null and null.getChild("first") will cause a NullPointerException. you have to check for these conditions in your code.
About | Sitemap | Contact