Thanks for the reply Erik. However, my follow up question is this: What about the block of code that appears in the startElement() method:
if(atts.getLength()>0){ attributeMap=new HashMap(); Do we need to modify it at all in anyway? Your suggestion works, but isn't this redundant? Just wondering. Fayyaz Erik Hatcher wrote: > > Fayyaz - that is the incorrect action to take. That will cause > future documents to have fields from all previous ones! All you > have to do is: > > private HashMap attributeMap = new HashMap(); > > Erik > > > On Dec 6, 2007, at 9:53 PM, syedfa wrote: > >> >> Thanks very much for your reply. I commented out the line: >> >> attributeMap.clear(); >> >> in the startElement() method, and the code ran! >> >> Thanks for your prompt reply, and my apologies for the delay in >> responding. >> >> All the best. >> Fayyaz >> >> >> Michael McCandless-2 wrote: >>> >>> >>> I think you need to initialize attributeMap, eg add " = new HashMap >>> ()" in >>> the declaration? >>> >>> Mike >>> >>> "syedfa" <[EMAIL PROTECTED]> wrote: >>>> >>>> Dear Fellow Java & Lucene developers: >>>> >>>> I am a Java developer learning lucene and I am currently going >>>> through >>>> the >>>> book Lucene in Action. At present, I am trying to run the sample >>>> code >>>> for >>>> indexing an xml document using sax. My code has been slightly >>>> updated >>>> for >>>> Lucene version 2.2: >>>> >>>> /* >>>> * To change this template, choose Tools | Templates >>>> * and open the template in the editor. >>>> */ >>>> >>>> package lucenexml; >>>> >>>> >>>> import java.io.File; >>>> import java.io.FileInputStream; >>>> import java.io.InputStream; >>>> import java.io.IOException; >>>> import java.util.HashMap; >>>> import java.util.Iterator; >>>> >>>> import org.xml.sax.helpers.DefaultHandler; >>>> import org.xml.sax.SAXException; >>>> import org.xml.sax.Attributes; >>>> >>>> import javax.xml.parsers.SAXParser; >>>> import javax.xml.parsers.SAXParserFactory; >>>> import javax.xml.parsers.ParserConfigurationException; >>>> >>>> >>>> import org.apache.lucene.document.Document; >>>> import org.apache.lucene.document.Field; >>>> >>>> /** >>>> * >>>> * @author fayyaz >>>> */ >>>> public class SAXXMLHandler extends DefaultHandler implements >>>> DocumentHandler{ >>>> >>>> private StringBuffer elementBuffer=new StringBuffer(); >>>> private HashMap attributeMap; >>>> >>>> private Document doc; >>>> /** >>>> * @param args the command line arguments >>>> */ >>>> public static void main(String[] args) throws Exception { >>>> // TODO code application logic here >>>> SAXXMLHandler handler=new SAXXMLHandler(); >>>> Document doc=handler.getDocument(new FileInputStream(new >>>> File(args[0]))); >>>> System.out.println(doc); >>>> } >>>> >>>> public Document getDocument(InputStream is) throws >>>> DocumentHandlerException{ >>>> >>>> SAXParserFactory spf=SAXParserFactory.newInstance(); >>>> >>>> try{ >>>> SAXParser parser=spf.newSAXParser(); >>>> parser.parse(is, this); >>>> >>>> } >>>> catch (IOException e){ >>>> throw new DocumentHandlerException("Cannot parse XML >>>> document", >>>> e); >>>> } >>>> catch (ParserConfigurationException e) { >>>> throw new DocumentHandlerException("Cannot parse XML >>>> document", >>>> e); >>>> } >>>> catch (SAXException e){ >>>> throw new DocumentHandlerException("Cannot parse XML >>>> document", >>>> e); >>>> } >>>> return doc; >>>> } >>>> >>>> public void startDocument(){ >>>> doc=new Document(); >>>> } >>>> >>>> public void startElement(String uri, String localName, String >>>> qName, >>>> Attributes atts) throws SAXException{ >>>> >>>> elementBuffer.setLength(0); >>>> attributeMap.clear(); >>>> if(atts.getLength()>0){ >>>> attributeMap=new HashMap(); >>>> for(int i=0; i<atts.getLength(); i++){ >>>> attributeMap.put(atts.getQName(i), atts.getValue >>>> (i)); >>>> } >>>> } >>>> } >>>> >>>> public void characters(char[] text, int start, int length){ >>>> elementBuffer.append(text, start, length); >>>> } >>>> >>>> public void endElement(String uri, String localName, String >>>> qName) >>>> throws SAXException{ >>>> if(qName.equals("address-book")){ >>>> return; >>>> } >>>> >>>> else if(qName.equals("contact")){ >>>> Iterator iter=attributeMap.keySet().iterator(); >>>> while(iter.hasNext()){ >>>> String attName=(String)iter.next(); >>>> String attValue=(String)attributeMap.get(attName); >>>> doc.add(new Field(qName, elementBuffer.toString(), >>>> Field.Store.YES,Field.Index.NO)); >>>> } >>>> } >>>> } >>>> } >>>> >>>> >>>> However, although the code compiles, I get the following runtime >>>> error >>>> when >>>> I pass the file addressbook.xml (which is used in the book) as a >>>> runtime >>>> argument: >>>> >>>> Exception in thread "main" java.lang.NullPointerException >>>> at lucenexml.SAXXMLHandler.startElement >>>> (SAXXMLHandler.java:81) >>>> at >>>> org.apache.xerces.parsers.AbstractSAXParser.startElement >>>> (Unknown >>>> Source) >>>> at >>>> org.apache.xerces.impl.dtd.XMLDTDValidator.startElement >>>> (Unknown >>>> Source) >>>> at >>>> org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanStartEleme >>>> nt(Unknown >>>> Source) >>>> at >>>> org.apache.xerces.impl.XMLDocumentScannerImpl >>>> $ContentDispatcher.scanRootElementHook(Unknown >>>> Source) >>>> at >>>> org.apache.xerces.impl.XMLDocumentFragmentScannerImpl >>>> $FragmentContentDispatcher.dispatch(Unknown >>>> Source) >>>> at >>>> org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument >>>> (Unknown >>>> Source) >>>> at org.apache.xerces.parsers.XML11Configuration.parse >>>> (Unknown >>>> Source) >>>> at org.apache.xerces.parsers.XML11Configuration.parse >>>> (Unknown >>>> Source) >>>> at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) >>>> at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown >>>> Source) >>>> at >>>> org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse >>>> (Unknown >>>> Source) >>>> at org.apache.xerces.jaxp.SAXParserImpl.parse(Unknown >>>> Source) >>>> at javax.xml.parsers.SAXParser.parse(SAXParser.java:198) >>>> at lucenexml.SAXXMLHandler.getDocument(SAXXMLHandler.java: >>>> 59) >>>> at lucenexml.SAXXMLHandler.main(SAXXMLHandler.java:49) >>>> Java Result: 1 >>>> >>>> What am I doing wrong? Any help would be greatly appreciated. >>>> >>>> Thanks in advance. >>>> Sincerely; >>>> Fayyaz >>>> >>>> -- >>>> View this message in context: >>>> http://www.nabble.com/Error-running-Lucene-in-Action-code- >>>> tf4947242.html#a14164565 >>>> Sent from the Lucene - Java Users mailing list archive at >>>> Nabble.com. >>>> >>>> >>>> -------------------------------------------------------------------- >>>> - >>>> To unsubscribe, e-mail: [EMAIL PROTECTED] >>>> For additional commands, e-mail: [EMAIL PROTECTED] >>>> >>> >>> --------------------------------------------------------------------- >>> To unsubscribe, e-mail: [EMAIL PROTECTED] >>> For additional commands, e-mail: [EMAIL PROTECTED] >>> >>> >>> >> >> -- >> View this message in context: http://www.nabble.com/Error-running- >> Lucene-in-Action-code-tf4947242.html#a14205907 >> Sent from the Lucene - Java Users mailing list archive at Nabble.com. >> >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: [EMAIL PROTECTED] >> For additional commands, e-mail: [EMAIL PROTECTED] > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > > -- View this message in context: http://www.nabble.com/Error-running-Lucene-in-Action-code-tf4947242.html#a14218817 Sent from the Lucene - Java Users mailing list archive at Nabble.com. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]