Results 1 to 2 of 2
  1. #1
    stephanruhl is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Exclamation Indexing HTML with Lucene, null pointer exception

    Java Code:
    package lucene.html.search;
    
    import java.io.File;
    import java.io.FileFilter;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import org.apache.lucene.analysis.standard.StandardAnalyzer;
    import org.apache.lucene.document.Document;
    import org.apache.lucene.document.Field;
    import org.apache.lucene.index.CorruptIndexException;
    import org.apache.lucene.index.IndexWriter;
    import org.apache.lucene.store.Directory;
    import org.apache.lucene.store.FSDirectory;
    import org.apache.lucene.util.Version;
    
    /**
     *
     * @author deniz
     */
    public class HTMLIndex {
        
        private IndexWriter writer;
    
        public static void main(String[] args) throws IOException{
    
            String indexDir,dataDir;
            int numIndexed ;
            indexDir = "Users/deniz/Desktop/Busway";
            dataDir = "Users/deniz/Desktop/ash-bus-each/2011-02-10/buses";
    
            System.out.println("Index will be created in : "+ indexDir);
            System.out.println("Data folder is :" + dataDir);
    
            HTMLIndex indexer = new HTMLIndex(indexDir);
    
            try{
                numIndexed = indexer.index(dataDir);
            }catch(Exception e){
    
           
            }
            finally{
                indexer.close();
            }
    
        }
    
        public HTMLIndex(String indexDir) throws IOException{
    
            Directory dir = FSDirectory.open(new File(indexDir));
            writer = new IndexWriter(dir,new StandardAnalyzer(Version.LUCENE_30), IndexWriter.MaxFieldLength.UNLIMITED);
        }
    
        public void close() throws IOException{
            writer.close();
        }
    
        public int index(String dataDir ) throws IOException {
           [B][SIZE="5"] File[] files;
            try{
             files= new File(dataDir).listFiles();
               
            for(File f : files){
                if (!f.isDirectory() &&!f.isHidden() && f.exists() && f.canRead()) {
                    indexFile(f);
                }
    
            }[/SIZE][/B]
            
            }catch(Exception e){
                e.printStackTrace();
            
            }
    
        return writer.numDocs();
            
        }
    
    
        protected Document getDocument(File f) throws Exception{
            Document doc = new Document();
            doc.add(new Field("contents", new FileReader(f)));
            doc.add(new Field("filename", f.getName(),Field.Store.YES,Field.Index.NOT_ANALYZED));
            return doc;
        }
    
        private void indexFile(File f) throws Exception {
            System.out.println("Indexing for : " + f.getCanonicalPath());
            Document doc = getDocument(f);
            writer.addDocument(doc);
            System.out.println(f.getCanonicalPath() + " was indexed");
        }
    
    
    
    }
    It keeps showing me null pointer exception for the highlighted part of the code...

    Any ideas from anyone?
    Last edited by Fubarable; 03-14-2011 at 02:25 AM. Reason: code tags added

  2. #2
    stephanruhl is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    indexDir = "Users/deniz/Desktop/Busway";
    dataDir = "Users/deniz/Desktop/ash-bus-each/2011-02-10/buses";


    there must be / at the begining of these paths :-D
    silly mistake of mine...

Similar Threads

  1. Null pointer exception
    By Domo230 in forum New To Java
    Replies: 4
    Last Post: 02-28-2011, 10:21 AM
  2. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  3. Help with Null Pointer Exception
    By Beginner in forum New To Java
    Replies: 2
    Last Post: 04-17-2010, 04:41 PM
  4. Null pointer exception?
    By coffee in forum New To Java
    Replies: 4
    Last Post: 08-03-2009, 03:22 AM
  5. null pointer exception
    By jyothi.priyanka in forum New To Java
    Replies: 12
    Last Post: 03-11-2009, 05:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •