Error trying to define or read the value of variables already defined - System.Nullreferenceexception

Asked

Viewed 80 times

1

I’m having trouble with this code. Whenever it rotates, it causes an exception of the type 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 in these two lines (if I remove the first, the exception occurs in the second):

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

Specifically when I try to take the value of word[0], And on the second line with the variable syntaxCount, but none of them has the null value, as you can see in the image below:

The variable "s" is just a row of a Richeditbox, word[0] has a value, so why is causing the exception NullReferenceException? syntaxCount has a value too :/

  • 2

    http://answall.com/help/mcve

2 answers

1

I played the code here and realized the following:

wordBuffer = null; //is an array, change by the code below or in the way you think best

var wordBuffer = new Array[20];

Match(s) //(s) did not see the statement maybe it is more above the code anyway See the example below

string s = "ab!@#$#@!";

Good luck. Sync and corrections by n17t01

  • 1

    Thank you, but I realized I was doing it wrong from the start. Apart from this obvious mistake, I was unaware of the total of strings which would be added to the array, however the correct approximation would be a List<>. So it would be even easier to handle and with the cleaner code.

0


You set the variable wordBuffer null.

wordBuffer = null;

and then tries to use it to store a word.

wordBuffer[syntaxCount] = word;

You must replace the line wordBuffer = null; for something like that: wordBuffer = new string[{tamanho do vetor}], replacing {tamanho do vetor} by the desired size for the vector.

  • As I commented in the answer above, I was unaware of the total amount of items that would be added to the array, so it would not be possible to set a size for it. The best thing that came to my attention was a List<>. Your explanation of what happened was more complete, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.