对象引用未设置为对象c#代码的实例

对象引用未设置为对象c#代码的实例,c#,object-reference,C#,Object Reference,我一直在玩这个代码,但是, 对象引用未设置为对象的实例 一个是有记号的箭头。谁能看看我做错了什么?我只发布了相关代码 谢谢 错误在//我的错误在这里 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using Sys

我一直在玩这个代码,但是, 对象引用未设置为对象的实例 一个是有记号的箭头。谁能看看我做错了什么?我只发布了相关代码

谢谢

错误在//我的错误在这里

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
namespace pkb
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>      
    /// 


    public partial class MainWindow : Window
    {

        string[] kbsubject = new string[4000];
        string[] kbbody = new string[4000];
        string[] wordsplit = new string[4000];
        int[] hits = new int[4000];
         StreamWriter WriteBody = new StreamWriter("kbsubjecttest.txt");
        StreamReader readSubject = new StreamReader("kbsubject.txt");
        StreamReader readBody = new StreamReader("kbbody.txt");
        int IndexHolder = 0, counter = 0, counterSearch = 0, WordsIndex = 0, counterWord=0, ArrayIndex = 0;
        string compareBody, compareSubject;


        public MainWindow()
        {
            InitializeComponent();




        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            wordsplit = SearchBox.Text.Split(' ');


            WordsIndex = 0;
            counterWord = 0;
            ArrayIndex = 0;
            counterSearch = 0;
            diagWindow.Items.Add(" counter = " + counter);
            while (counter > counterSearch)
            {                                                             // MY ERROR BELOW
                if (kbbody[counterWord].Contains(wordsplit[ArrayIndex])) // MY ERROR HERE
                {
                    hits[ArrayIndex] = counterWord;
                    diagWindow.Items.Add(hits[ArrayIndex] + " " + kbbody[ArrayIndex]);
                    ArrayIndex++;

                }

                counterWord++;
                WordsIndex++;
                counterSearch++;
                diagWindow.Items.Add(" we are on index " + counterSearch);





            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            compareBody = readBody.ReadLine();
            compareSubject = readSubject.ReadLine();
            if (compareBody == compareSubject)
            {
                diagWindow.Items.Add(" Subject and Body Database compared successfully.");
                IndexHolder = int.Parse(compareBody);
                diagWindow.Items.Add(IndexHolder + " Index saved to variable.");

            }
            else
            {
                diagWindow.Items.Add("Error with comparison");
                diagWindow.Items.Add("------------------------------");
                diagWindow.Items.Add("Body comparison has " + compareBody + " where Subject comparison has " + compareSubject);
            }

            //load information into arrays



            while (counter <= IndexHolder)
            {
                kbbody[counter] = readBody.ReadLine();
                kbsubject[counter] = readSubject.ReadLine();
                counter++;


            }
            diagWindow.Items.Add(counter + " Items successfully added into searchable database");
            diagWindow.Items.Add(" counter = " + counter);





        }
        }
    }
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用System.IO;
命名空间pkb
{
/// 
///MainWindow.xaml的交互逻辑
///       
/// 
公共部分类主窗口:窗口
{
字符串[]kbsubject=新字符串[4000];
字符串[]kbbody=新字符串[4000];
string[]wordsplit=新字符串[4000];
int[]hits=新int[4000];
StreamWriter WriteBody=新的StreamWriter(“kbsubjecttest.txt”);
StreamReader readSubject=新的StreamReader(“kbsubject.txt”);
StreamReader readBody=新的StreamReader(“kbbody.txt”);
int IndexHolder=0,counter=0,counterSearch=0,WordsIndex=0,counterWord=0,ArrayIndex=0;
字符串compareBody,compareSubject;
公共主窗口()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,路由目标)
{
wordsplit=SearchBox.Text.Split(“”);
WordsIndex=0;
反字=0;
ArrayIndex=0;
反搜索=0;
diagWindow.Items.Add(“计数器=”+计数器);
while(计数器>反搜索)
{//下面是我的错误
if(kbbody[counterWord].Contains(wordsplit[ArrayIndex])//我的错误在这里
{
点击[ArrayIndex]=反词;
diagWindow.Items.Add(点击[ArrayIndex]+“”+kbbody[ArrayIndex]);
ArrayIndex++;
}
反词++;
WordsIndex++;
反搜索++;
diagWindow.Items.Add(“我们在索引上”+反搜索);
}
}
已加载私有无效窗口(对象发送器、路由目标)
{
compareBody=readBody.ReadLine();
compareSubject=readSubject.ReadLine();
if(compareBody==compareSubject)
{
diagWindow.Items.Add(“主题和主体数据库比较成功”);
IndexHolder=int.Parse(compareBody);
添加(IndexHolder+“保存到变量的索引”);
}
其他的
{
diagWindow.Items.Add(“比较错误”);
diagWindow.Items.Add(“-----------------------------------”);
diagWindow.Items.Add(“主体比较有”+比较主体+”,其中主体比较有“+比较主体”);
}
//将信息加载到数组中

而(counter假设我输入“test”并且在“”周围拆分它时没有空格,wordsplit数组中将有一个元素,在第二次迭代时,将没有其他元素,所以我猜您在那里会出错。

您不认为有可能是kbbody[counterWord]是空的…堆栈跟踪中指示了哪一行?根据我对它的读取,我将它运行到一个列表框中..如果DataAgreement与@astander一致,那么从读取文件到使用它的值缺少检查索引会导致太多问题,您不是来自这里吗