The following program is giving an error:
System.NullReferenceException Object Reference not set to an instance of an object.

at IgnoreKwic.GetAllLinesForIndexWords<with parameters>
at IgnoreKwic.BuildOutput<with parameters>
at IgnoreKwic.Main<>

In the line where I wrote that I want to display the output on the screen, would this work?

using System.Collections.Generic;// Adds definition of List<T>
using System.IO; // Adds file I/O definitions
using System.Text; // Adds StringBuilder definition.

public class IgnoreKwic
{
static void Main()
{
string textPath = "Lines.txt";
string ignoredPath = "IgnoredWords.txt";

GetTextLines(textPath);
GetIgnoredWords(ignoredPath);
string output = BuildOutput(ignoredPath, textPath);

// *** Now I want to write the output to the screen.
while(string.Console.Write(output);
}

static void GetTextLines(string path)
{
StreamWriter sw = new StreamWriter(path);
string fileLine;
Console.Write("Please enter the text to be examined ");
fileLine = Console.ReadLine();
while (fileLine.Length > 0)
{
sw.WriteLine(fileLine);
fileLine = Console.ReadLine();
}
sw.Flush();
sw.Close();
}
static void GetIgnoredWords(string path)
{
StreamWriter sw = new StreamWriter(path);
Console.Write("Please enter the words to be ignored. Please enter words in single line seperated by spaces ");

string wordList = Console.ReadLine();
string[] words = wordList.Split(' ');
for (int i = 0; i < words.Length; i++)
{
sw.WriteLine(words);
}
sw.Flush();
sw.Close();
}

static string BuildOutput(string ignorePath, string inputPath)
{
StringBuilder sb = new StringBuilder(1024);
StreamReader sr = new StreamReader(ignorePath);

List<string> ignoredWords = new List<string>();
string text;
while ((text = sr.ReadLine()) != null)
{
ignoredWords.Add(text);
}

StreamReader sr2 = new StreamReader(inputPath);
List<string> inputs = new List<string>();

while ((text = sr2.ReadLine()) != null)
{
inputs.Add(text);
}
//kaahinm@cs.umb.edu

// Get the index words from the set of inputs
List<string> indexWords = FindAllWordsForCaps(inputs, ignoredWords);
List<string> currentLines = null;
List<int> offsets = null;
int maxOffset;
string pad = string.Empty;

foreach (string indexWord in indexWords)
{
maxOffset = -1;
currentLines = GetAllLinesForIndexWord(inputs, indexWord, ref offsets);
for (int i = 0; i < offsets.Count; i++)
{
if (offsets > maxOffset)
maxOffset = offsets;
}
for (int j = 0; j < currentLines.Count; j++)
{
pad = new string(' ', maxOffset - offsets[j]);
sb.Append(pad);
if (offsets[j] > 0)
{
sb.Append(currentLines[j].Substring(0, offsets[j]));
}
sb.Append(currentLines[j].Substring(offsets[j], indexWord.Length).ToUpper());
if (currentLines[j].Length > (offsets[j] + indexWord.Length))
{
sb.Append(currentLines[j].Substring(offsets[j] + indexWord.Length));
}
}
}
return sb.ToString();
}
private static List<string> GetAllLinesForIndexWord(List<string> inputLines, string indexWord, ref List<int> offsets)
{
List<string> output = new List<string>(10);
string lowIndex = indexWord.ToLower();
int pos = 0;

foreach (string line in inputLines)
{
pos = (line.ToLower()).IndexOf(lowIndex);
if (pos > -1)
{
output.Add(line);
offsets.Add(pos);
}
}
return output;
}
private static List<string> FindAllWordsForCaps(List<string> inputLines, List<string> ignoredWords)
{
List<string> indexWords = new List<string>(64);
string[] allCurrent = null;
string current;
int currentIndex = 0;
int inputIndex = 0;
int ignoredIndex = 0;
string actualCurrent = string.Empty;

while (inputIndex < inputLines.Count)
{
allCurrent = inputLines[inputIndex++].Split(' ');
currentIndex = 0;
while (currentIndex < allCurrent.Length)
{
current = allCurrent[currentIndex++];
actualCurrent = current;
if (current.EndsWith(","))
current = current.Substring(0, current.Length - 1);
ignoredIndex = 0;
while (ignoredIndex < ignoredWords.Count)
{
if (string.Compare(current, ignoredWords[ignoredIndex], true) == 0)
break;
ignoredIndex++;
}
if (ignoredIndex < ignoredWords.Count) // This one gets caps
indexWords.Add(actualCurrent);
}
}
return indexWords;
}
}

You should initialize all object before using, I didn't see your code, but error message says that!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.