我一直試圖玩這個代碼,但我得到, 對象引用未設置爲對象的實例 一個標記的箭頭。任何人都可以看看我做錯了什麼?我只發佈了相關的代碼。對象引用未設置爲對象的實例c# - 代碼發佈
謝謝!
ERROR AT //我的錯誤這裏
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);
}
}
}
它可能,你不覺得,** kbbody [counterWord] **是** null ** ... –
哪一行顯示在堆棧跟蹤中? – codemonkeh
根據我讀過的內容,我將它運行到一個列表框中..有數據 – user2881231