The following program is giving an error:
System.NullRefe renceException Object Reference not set to an instance of an object.
at IgnoreKwic.GetA llLinesForIndex Words<with parameters>
at IgnoreKwic.Buil dOutput<with parameters>
at IgnoreKwic.Main <>
In the line where I wrote that I want to display the output on the screen, would this work?
[code=cpp]
using System.Collecti ons.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.t xt";
GetTextLines(te xtPath);
GetIgnoredWords (ignoredPath);
string output = BuildOutput(ign oredPath, textPath);
// *** Now I want to write the output to the screen.
while(string.Co nsole.Write(out put);
}
static void GetTextLines(st ring path)
{
StreamWriter sw = new StreamWriter(pa th);
string fileLine;
Console.Write(" Please enter the text to be examined ");
fileLine = Console.ReadLin e();
while (fileLine.Lengt h > 0)
{
sw.WriteLine(fi leLine);
fileLine = Console.ReadLin e();
}
sw.Flush();
sw.Close();
}
static void GetIgnoredWords (string path)
{
StreamWriter sw = new StreamWriter(pa th);
Console.Write(" Please enter the words to be ignored. Please enter words in single line seperated by spaces ");
string wordList = Console.ReadLin e();
string[] words = wordList.Split( ' ');
for (int i = 0; i < words.Length; i++)
{
sw.WriteLine(wo rds[i]);
}
sw.Flush();
sw.Close();
}
static string BuildOutput(str ing ignorePath, string inputPath)
{
StringBuilder sb = new StringBuilder(1 024);
StreamReader sr = new StreamReader(ig norePath);
List<string> ignoredWords = new List<string>();
string text;
while ((text = sr.ReadLine()) != null)
{
ignoredWords.Ad d(text);
}
StreamReader sr2 = new StreamReader(in putPath);
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 = FindAllWordsFor Caps(inputs, ignoredWords);
List<string> currentLines = null;
List<int> offsets = null;
int maxOffset;
string pad = string.Empty;
foreach (string indexWord in indexWords)
{
maxOffset = -1;
currentLines = GetAllLinesForI ndexWord(inputs , indexWord, ref offsets);
for (int i = 0; i < offsets.Count; i++)
{
if (offsets[i] > maxOffset)
maxOffset = offsets[i];
}
for (int j = 0; j < currentLines.Co unt; j++)
{
pad = new string(' ', maxOffset - offsets[j]);
sb.Append(pad);
if (offsets[j] > 0)
{
sb.Append(curre ntLines[j].Substring(0, offsets[j]));
}
sb.Append(curre ntLines[j].Substring(offs ets[j], indexWord.Lengt h).ToUpper());
if (currentLines[j].Length > (offsets[j] + indexWord.Lengt h))
{
sb.Append(curre ntLines[j].Substring(offs ets[j] + indexWord.Lengt h));
}
}
}
return sb.ToString();
}
private static List<string> GetAllLinesForI ndexWord(List<s tring> inputLines, string indexWord, ref List<int> offsets)
{
List<string> output = new List<string>(10 );
string lowIndex = indexWord.ToLow er();
int pos = 0;
foreach (string line in inputLines)
{
pos = (line.ToLower() ).IndexOf(lowIn dex);
if (pos > -1)
{
output.Add(line );
offsets.Add(pos );
}
}
return output;
}
private static List<string> FindAllWordsFor Caps(List<strin g> 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.Coun t)
{
allCurrent = inputLines[inputIndex++].Split(' ');
currentIndex = 0;
while (currentIndex < allCurrent.Leng th)
{
current = allCurrent[currentIndex++];
actualCurrent = current;
if (current.EndsWi th(","))
current = current.Substri ng(0, current.Length - 1);
ignoredIndex = 0;
while (ignoredIndex < ignoredWords.Co unt)
{
if (string.Compare (current, ignoredWords[ignoredIndex], true) == 0)
break;
ignoredIndex++;
}
if (ignoredIndex < ignoredWords.Co unt) // This one gets caps
indexWords.Add( actualCurrent);
}
}
return indexWords;
}
}[/code]
System.NullRefe renceException Object Reference not set to an instance of an object.
at IgnoreKwic.GetA llLinesForIndex Words<with parameters>
at IgnoreKwic.Buil dOutput<with parameters>
at IgnoreKwic.Main <>
In the line where I wrote that I want to display the output on the screen, would this work?
[code=cpp]
using System.Collecti ons.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.t xt";
GetTextLines(te xtPath);
GetIgnoredWords (ignoredPath);
string output = BuildOutput(ign oredPath, textPath);
// *** Now I want to write the output to the screen.
while(string.Co nsole.Write(out put);
}
static void GetTextLines(st ring path)
{
StreamWriter sw = new StreamWriter(pa th);
string fileLine;
Console.Write(" Please enter the text to be examined ");
fileLine = Console.ReadLin e();
while (fileLine.Lengt h > 0)
{
sw.WriteLine(fi leLine);
fileLine = Console.ReadLin e();
}
sw.Flush();
sw.Close();
}
static void GetIgnoredWords (string path)
{
StreamWriter sw = new StreamWriter(pa th);
Console.Write(" Please enter the words to be ignored. Please enter words in single line seperated by spaces ");
string wordList = Console.ReadLin e();
string[] words = wordList.Split( ' ');
for (int i = 0; i < words.Length; i++)
{
sw.WriteLine(wo rds[i]);
}
sw.Flush();
sw.Close();
}
static string BuildOutput(str ing ignorePath, string inputPath)
{
StringBuilder sb = new StringBuilder(1 024);
StreamReader sr = new StreamReader(ig norePath);
List<string> ignoredWords = new List<string>();
string text;
while ((text = sr.ReadLine()) != null)
{
ignoredWords.Ad d(text);
}
StreamReader sr2 = new StreamReader(in putPath);
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 = FindAllWordsFor Caps(inputs, ignoredWords);
List<string> currentLines = null;
List<int> offsets = null;
int maxOffset;
string pad = string.Empty;
foreach (string indexWord in indexWords)
{
maxOffset = -1;
currentLines = GetAllLinesForI ndexWord(inputs , indexWord, ref offsets);
for (int i = 0; i < offsets.Count; i++)
{
if (offsets[i] > maxOffset)
maxOffset = offsets[i];
}
for (int j = 0; j < currentLines.Co unt; j++)
{
pad = new string(' ', maxOffset - offsets[j]);
sb.Append(pad);
if (offsets[j] > 0)
{
sb.Append(curre ntLines[j].Substring(0, offsets[j]));
}
sb.Append(curre ntLines[j].Substring(offs ets[j], indexWord.Lengt h).ToUpper());
if (currentLines[j].Length > (offsets[j] + indexWord.Lengt h))
{
sb.Append(curre ntLines[j].Substring(offs ets[j] + indexWord.Lengt h));
}
}
}
return sb.ToString();
}
private static List<string> GetAllLinesForI ndexWord(List<s tring> inputLines, string indexWord, ref List<int> offsets)
{
List<string> output = new List<string>(10 );
string lowIndex = indexWord.ToLow er();
int pos = 0;
foreach (string line in inputLines)
{
pos = (line.ToLower() ).IndexOf(lowIn dex);
if (pos > -1)
{
output.Add(line );
offsets.Add(pos );
}
}
return output;
}
private static List<string> FindAllWordsFor Caps(List<strin g> 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.Coun t)
{
allCurrent = inputLines[inputIndex++].Split(' ');
currentIndex = 0;
while (currentIndex < allCurrent.Leng th)
{
current = allCurrent[currentIndex++];
actualCurrent = current;
if (current.EndsWi th(","))
current = current.Substri ng(0, current.Length - 1);
ignoredIndex = 0;
while (ignoredIndex < ignoredWords.Co unt)
{
if (string.Compare (current, ignoredWords[ignoredIndex], true) == 0)
break;
ignoredIndex++;
}
if (ignoredIndex < ignoredWords.Co unt) // This one gets caps
indexWords.Add( actualCurrent);
}
}
return indexWords;
}
}[/code]
Comment