网页搜索的时候报错:未将对象引用设置到对象实例
刚开始学lucene.netzz做搜索~~研究的是别人的代码,就是龙行天下搜索的那一个。有一个地方报错了,代码如下:
using System;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using KTDictSeg;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
// 声明命名空间
// 分词
public partial class Search : Page
{
public static CSimpleDictSeg simpleDictSeg;
// 将关键分词,存储在数组中
private static string[] strKey;
private readonly DataTable results = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
private void BindGridView()
{
// 获取地址栏传值,并为其解码
string strKeyWord = Server.UrlDecode(Request.QueryString["Key"]);
// 将查询关键字,显示在查询文本框中
txtKey.Text = strKeyWord;
// 设置查询结果页标题
Title = txtKey.Text + "经验技巧搜索";
// 调用自定义FindResult方法,将查询结果显示在GridView控件中
GridView1.DataSource = FindResult(strKeyWord);
GridView1.DataKeyNames = new[] {"id"};
GridView1.DataBind();
}
// 将存储查询结果的数据源DataTable对象
//初始化数据源DataTable对象,将查询结果保存到数据源对象中
private void SetTable()
{
// 创建结果记录集
// 定义字段
results.Columns.Add("resTitle", typeof (string));
results.Columns.Add("resContent", typeof (string));
results.Columns.Add("id", typeof (string));
}
// 执行查询
private DataTable FindResult(string keyword)
{
// 调用自定义方法,初始化数据源对象
// 创建查询结果存储表
SetTable();
// 关键字分词
FC(keyword);
// 开始记时
DateTime dt = DateTime.Now;
// 把刚才建立的索引取出来
var search = new IndexSearcher(Server.MapPath("Index"));
// 初始化长度
var sField = new string[strKey.Length];
sField.SetValue("resTitle", 0);
// 创建将要查询resTitle字段
for (int s = 1; s < strKey.Length; s++)
{
sField.SetValue("resTitle", s);
}
// 清空分词结果
lblFC.Text = "";
// 显示分词结果
foreach (string k in strKey)
{
// 显示分词结果在2个以上的词,并设置超级链接。
if (k.Length > 1)
lblFC.Text = lblFC.Text + " " +
"<a target='black' href='Search.aspx?Key=" + k + "'>" + k + "</a>";
}
// 执行查询
var q = new BooleanQuery();
Query qq = MultiFieldQueryParser.Parse(strKey, sField, new StandardAnalyzer());
q.Add(qq, BooleanClause.Occur.MUST);
// 获取查询结果
Hits hit = search.Search(q);
// 计算用时
TimeSpan ts = DateTime.Now.Subtract(dt);
for (int i = 0; i <= hit.Length() - 1; i++)
{
Document doc = hit.Doc(i);
DataRow row = results.NewRow();
// 添加到DataRow中
// 显示高亮描红查询关键字
row["resTitle"] = HightLight(Server.UrlDecode(Request.QueryString["key"]), doc.Get("resTitle"));
row["resContent"] = doc.Get("resContent");
row["id"] = doc.Get("id");
results.Rows.Add(row);
}
search.Close();
lblSum.Text = "<font color=red>找到相关查询结果约" + hit.Length() + "篇,</font> 用时" + ts.ToString() + " 秒 ";
return results;
}
出错的地方用红色标出了
就是var sField = new string[strKey.Length];这一句报了一个未将对象引用设置到对象实例的错误。。。。。我觉得是不是开始的时候那个private static string[] strKey;并没有赋值啊~~求各位大神解答!!!