c# QUESTION

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • asif08
    New Member
    • Feb 2008
    • 4

    c# QUESTION

    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]
    Last edited by Frinavale; Feb 12 '08, 09:30 PM. Reason: Added [code] tags
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.) Use code tags when posting code
    2.) In your GetAllLinesForI ndexWord method, Console.WriteLi ne all the variables that are passed to this method as the first step of the method(a.k.a the poor man's debugger). You'll catch that null easily that way. My money is on the offsets list.

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Also, what is this:
      Code:
      while(string.Console.Write(output);
      Console.Write() is not a member of the string class, nor does it return a value (it's void) so it's not valid in the boolean expression part of the while loop

      Comment

      • asif08
        New Member
        • Feb 2008
        • 4

        #4
        Originally posted by r035198x
        1.) Use code tags when posting code
        2.) In your GetAllLinesForI ndexWord method, Console.WriteLi ne all the variables that are passed to this method as the first step of the method(a.k.a the poor man's debugger). You'll catch that null easily that way. My money is on the offsets list.

        BINGO!!!
        the error was because offsets was set to null!

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by asif08
          BINGO!!!
          the error was because offsets was set to null!
          With the power of Console.WriteLi ne, a null reference exception is as good as a sitting duck.
          <laughing all the way to the bank>

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            You could have used a debug session and the watch window to see it too.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by Plater
              You could have used a debug session and the watch window to see it too.
              These funky kids of today ...

              Comment

              Working...