lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Michael McCandless" <luc...@mikemccandless.com>
Subject Re: Error running Lucene in Action code
Date Wed, 05 Dec 2007 10:02:29 GMT

I think you need to initialize attributeMap, eg add " = new HashMap()" in
the declaration?

Mike

"syedfa" <fayyazuddin@gmail.com> 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.scanStartElement(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: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Mime
View raw message