Suppose i enter "test" and no space on splitting it around ' ' there will be one element in wordsplit array and on second iterate there ll' be no other element So i guess you get error there.
Object reference not set to an instance of an object c# - code posted
-
01-07-2022 - |
Question
I kept trying to play with this code, but am getting, Object reference not set to an instance of an object one the marked arrow. Can anyone look at what I'm doing wrong? I've posted only the relevant code.
Thanks!
ERROR AT // MY ERROR HERE
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);
}
}
}
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow