0

I'm having issues with this code. Everytime when it runs, it returns me the 'System.NullReferenceException'.

// Clear out the Array of code words
wordBuffer = null;
Int32 syntaxCount = 0;

// Create the regular expression object to match against the string
Regex defaultRegex = new Regex(@"\w+|[^A-Za-z0-9_ \f\t\v]",
RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match wordMatch;

// Loop through the string and continue to record
// words and symbols and their corresponding positions and lengths
for (wordMatch = defaultRegex.Match(s); wordMatch.Success; wordMatch = wordMatch.NextMatch())
{
    var word = new Object[3] { wordMatch.Value, wordMatch.Index, wordMatch.Length };
    wordBuffer[syntaxCount] = word;
    Debug.WriteLine("Found = " + word[0]);
    syntaxCount++;
}

// return the number of symbols and words
return syntaxCount;

The exception occurs on those lines:

Debug.WriteLine("Found = " + word[0]);
                syntaxCount++;

Specifically when trying to get the word[0] value, and on the second line with the syntaxCount, but none of those values are null, as you can see in the image below:

The variable "s" is just a line of a RichEditBox, word[0] has a value, so why is it returning the NullReferenceException? syntaxCount has a value too :/

  • 2
    `wordBuffer = null;`. I'm not sure why you see error on next line, but exception should happen (as described in duplicate) on previous `wordBuffer[syntaxCount] = word;` line. – Alexei Levenkov Feb 01 '15 at 01:51
  • Side note: thanks for effort put in the question - it is nice to see amount of information you've collected/presented. – Alexei Levenkov Feb 01 '15 at 01:54
  • 2
    I don't think this is a dupilcate, as the OP does not now how arrays are working. This is not only about NullRef Exp. – Olivier Jacot-Descombes Feb 01 '15 at 01:58
  • @OlivierJacot-Descombes Arrays are explicitly covered in [What is nullRef](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) too... Consider to update question to match your answer when you are done. – Alexei Levenkov Feb 01 '15 at 02:02
  • @AlexeiLevenkov I know how dificult it is to understand incomplete codes, so I try to make it easier adding how much info I can. :) – Marlon Santos Feb 01 '15 at 02:26

1 Answers1

2

You are getting the exception on the line wordBuffer[syntaxCount] = word;

You are using the wrong approach for storing the results. Arrays are not created automatically and they do not grow automatically. I.e., you need to define their size in advance with string[] arr = new string[size]. Use a list instead, as you do not know the size in advance here. Lists grow automatically:

// Initialize with
var wordBuffer = new List<string>();

// ...
// And then add a word to the list with
wordBuffer.Add(word);

You query the number of entries with wordBuffer.Count and you can access the items as in an array, once they have been added: wordBuffer[i], where the index goes from 0 to wordBuffer.Count - 1. This makes the variable syntaxCount superfluous.

And of course, you can loop through a list with foreach.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188