对象引用未设置为对象的实例c# - 已发布的代码

时间:2013-10-17 04:02:56

标签: 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 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);





        }
        }
    }

2 个答案:

答案 0 :(得分:0)

由于我无法发表评论,我认为您已确保SearchBox.Text包含至少包含一个空格的文本。似乎是空格的是空格(或者你在引号中所拥有的是一个“真正的”空间。只是一个想法。

答案 1 :(得分:0)

假设我输入“test”并且没有空格分开它''在wordsplit数组中会有一个元素,在第二次迭代时会有ll'没有其他元素所以我猜你在那里得到错误。