Can anyone help me out with the code. I'm getting the Null Reference Object Exception at the following line

textFile1_Paras[paraNumber] = line;

I'm trying to separate paragraphs from a text file into string array

int paraNumber = 0;
            string[] lines = File.ReadAllLines(filePath1);

            foreach (string line in lines)
            {
                while (line != string.Empty)
                {
                                      
                    textFile1_Paras[paraNumber] = line; 
                    
                }
                paraNumber++;
            }
            MessageBox.Show(textFile1_Paras[paraNumber]);

Dani AI

Generated

Good catch by : the immediate NullReferenceException came from using an array variable that was never initialized. Initializing an array requires a size, so either allocate it before use or use a dynamically sized collection such as List<string>. There are also a couple of other logic problems to watch for in this topic: the inner while never changes the loop variable (so it becomes an infinite loop), and reading or showing paraNumber after the loop can easily produce an out-of-range access.

A safer, more robust approach is to build paragraphs into a List while treating one-or-more blank lines as paragraph delimiters. Use string.IsNullOrWhiteSpace to detect blank lines (handles spaces/tabs), and File.ReadLines for large files so you do not load everything at once. After collecting paragraphs you can call ToArray() if you need an array.

Example implementation:

var paragraphs = new List<string>();
var sb = new StringBuilder();

foreach (var line in File.ReadLines(filePath1))
{
    if (string.IsNullOrWhiteSpace(line))
    {
        if (sb.Length > 0)
        {
            paragraphs.Add(sb.ToString().Trim());
            sb.Clear();
        }
    }
    else
    {
        if (sb.Length > 0) sb.AppendLine();
        sb.Append(line);
    }
}
if (sb.Length > 0) paragraphs.Add(sb.ToString().Trim());

if (paragraphs.Count > 0)
    MessageBox.Show(paragraphs[0]);

Notes: initialize your storage before assigning to it; avoid modifying loop control variables inside nested loops; always check counts/indices before accessing an element; prefer List<T> for unknown size. later reported it worked — the pattern above will make the code more reliable and easier to debug.

Recommended Answers

All 4 Replies

probably because you dont check that paraNumber < than the number of available lines. (eg you havent shown where you set textFile1_paras, as an array, it may not be big enough

below is the complete code. Have been trying to bug, didn't think if it has to do something with the paraNumbers < checks...

string[] textFile1_Paras = null;

        //Reading file contents to separate each individual paragraph into string
        private void identifyParas()
        {
            int paraNumber = 0;
            string[] lines = File.ReadAllLines(filePath1);

            foreach (string line in lines)
            {
                while (line != string.Empty)
                {
                    //string[] words = line.Split(delim, StringSplitOptions.RemoveEmptyEntries);
                    // here you've got all your words in that array. Do what you want with them
                    if (line != "")
                    {
                        textFile1_Paras[paraNumber] = line;
                    }
                    
                }
                paraNumber++;
            }
            MessageBox.Show(textFile1_Paras[paraNumber]);
        }

probably because you dont check that paraNumber < than the number of available lines. (eg you havent shown where you set textFile1_paras, as an array, it may not be big enough

That will be why then, your textFile1_paras is null. You havent initialised it.

Thanks, got that working

That will be why then, your textFile1_paras is null. You havent initialised it.

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.