这一行:
File[] FileOrDir=root.listFiles(); FileOrDir可能为null在这句后面需要检测 null的情况。
if(FileOrDir!=null){  //your code here
}

解决方案 »

  1.   

    忘了说了  更加自然的方式是 先判断这个文件是不是目录
    public void writerToIndex(String DirFile) 

      try 
      { 
      File root=new File(DirFile); 
      if(root.isDirectory()){
        File[] files=root.listFiles();
        for(File file :files){
           if(file.isDirectory()){
              this.writerToIndex(root.getPath());       }else{
               //do work for a simple file
     
           }
        }
          }else{
       
         //do work for a simple file 
      }
      

  2.   

    报错Unhandled exception type IOException,加上try catch就行了,应该是编译错误。
    public void IndexTest(IndexWriter writer) 

      try 
      { 
      writer = new IndexWriter(index_path,new StandardAnalyzer(),false); 
      System.out.println(writer.toString()); 
      } 
      catch(Exception e) 
      { 
      System.out.println("writer初始化错误"); 
      
      System.out.println("---"); 
      e.printStackTrace(); 
      } 

    这里函数的参数会被try后的第一句覆盖掉的啊。