| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 480 人关注过本帖
标题:数组指针异常处理,怎么改?
只看楼主 加入收藏
zwh811
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-9-23
收藏
 问题点数:0 回复次数:2 
数组指针异常处理,怎么改?
import
import
import
import
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermDocs;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;

public class Search {
    private String INDEX_STORE_PATH = "f:\\index";

    public void indexSearch(String searchType, String searchKey) {
        try {
            // 根据索引位置建立IndexSearcher
            IndexSearcher searcher = new IndexSearcher(INDEX_STORE_PATH);
            // 建立搜索单元,searchType代表要搜索的Filed,searchKey代表关键字
            Term t = new Term(searchType, searchKey);
            // 由Term生成一个Query
            Query q = new TermQuery(t);
            // 搜索开始时间
            Date beginTime = new Date();
            // 获取一个<document, frequency>的枚举对象TermDocs
            TermDocs termDocs = searcher.getIndexReader().termDocs(t);
            while (termDocs.next()) {
                // 输出在文档中出现关键词的次数
                System.out.println(termDocs.freq());
                // 输出搜索到关键词的文档
                // System.out.println(searcher.getIndexReader().document(termDocs.doc()));
            }
            // 搜索完成时间
            Date endTime = new Date();
            // 搜索所耗时间
            long timeOfSearch = endTime.getTime() - beginTime.getTime();
            System.out
                    .println("The time For indexsearch is " + timeOfSearch + " ms");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void StringSearch(String keyword, String searchDir) {
        File filesDir = new File(searchDir);
        // 返回目录文件夹所有文件数组
52行        File[] files= filesDir.listFiles();
        // HashMap保存文件名和匹配次数对
        Map rs = new HashMap();
        // 记录搜索开始时间
        Date beginTime = new Date();
        // 遍历所有文件
第59行        for (int i = 0; i < files.length; i++) {
            // 初始化匹配次数
            int hits = 0;
            try {
                // 读取文件内容
                BufferedReader br = new BufferedReader(new FileReader(files[i]));
                StringBuffer sb = new StringBuffer();
                String line = br.readLine();
                while (line != null) {
                    sb.append(line);
                    line = br.readLine();
                }
                br.close();
                // 将StringBuffer转化成String,以便于搜索
                String stringToSearch = sb.toString();
                // 初始化fromIndex
                int fromIndex = -keyword.length();
                // 逐个匹配关键词
                while ((fromIndex = stringToSearch.indexOf(keyword, fromIndex
                        + keyword.length())) != -1) {
                    hits++;
                }
                // 将文件名和匹配次数加入HashMap
                rs.put(files[i].getName(), new Integer(hits));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 输出查询结果
        Iterator it = rs.keySet().iterator();
        while (it.hasNext()) {
            String fileName = (String) it.next();
            Integer hits = (Integer) rs.get(fileName);
            System.out.println("find " + hits.intValue() + " matches in "
                    + fileName);
        }
        // 记录结束时间
        Date endTime = new Date();
        // 得到搜索耗费时间
        long timeOfSearch = endTime.getTime() - beginTime.getTime();
        System.out.println("The time For string search is " + timeOfSearch + " ms");
    }

}



Exception in thread "main" java.lang.NullPointerException
    at ch2.lucenedemo.process.Search.StringSearch(Search.java:59)
我知道是files.length异常,求救怎么改???
搜索更多相关主题的帖子: class public import 
2011-04-07 00:00
蓝色石头
Rank: 2
等 级:论坛游民
帖 子:13
专家分:70
注 册:2009-9-17
收藏
得分:0 
程序没错...你给人家空的路径.或是错误的路径.就会出来这种异常!!!
2011-04-07 13:45
zwh811
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-9-23
收藏
得分:0 
回复 2楼 蓝色石头
弄好了,谢谢啦。。。
2011-04-07 14:57
快速回复:数组指针异常处理,怎么改?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018109 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved