0

It is showing Null Reference Exception on line char[] myChar = read.ToCharArray();.

I am unable to figure out. Please help

class Program
{
    static void Main(string[] args)
    {
        StreamReader myReader = new StreamReader("TextFile1.txt");
        string read = "";

        while (read != null)
        {
            read = myReader.ReadLine();
            Console.WriteLine(read);

        }

       char[] myChar = read.ToCharArray();

        for (int i = 0; i < myChar.Length; i++)
        {
            Console.WriteLine(myChar[i]);
        }

        Console.WriteLine(read);
        myReader.Close();
        Console.ReadKey();
    }
}
5
  • 3
    Your while loop right above that is exiting only after read is null, so of course read.ToCharArray() will give the exception. Commented Sep 7, 2013 at 17:23
  • at that line, read will be null, because of the condition in the while loop. Commented Sep 7, 2013 at 17:24
  • Maybe you could tell us what you want the code from read.ToCharArray() to achieve? Commented Sep 7, 2013 at 17:26
  • I need to match my string with a regular expression and for that reason i need to convert the string into char array to match every character in the string. can you help me out with any other logic? Commented Sep 7, 2013 at 17:37
  • You should describe what each part of your program is doing. For example when does this loop end: while (read != null) For example, step 1: Read the entire file and print it out. Step 2 I want to access the file again; I'll have to restart at the beginning of the file. Commented Sep 7, 2013 at 18:06

2 Answers 2

1

The read should be null when the loop is finished and calling ToCharArray on null should give exception. you can put this statement in while loop. I believe you are trying to do some experimentation as you have already printed the string with Console.WriteLine(read);

while ((read = sr.ReadLine()) != null)
{      
    Console.WriteLine(read);
    char[] myChar = read.ToCharArray();
    for (int i = 0; i < myChar.Length; i++)        
        Console.WriteLine(myChar[i]);       
}
Sign up to request clarification or add additional context in comments.

Comments

0
        StringBuilder sb = new StringBuilder();
        using (StreamReader sr = new StreamReader("TestFile.txt")) 
        {
            string line;
            // Read and display lines from the file until the end of 
            // the file is reached.
            while ((line = sr.ReadLine()) != null) 
            {
                Console.WriteLine(line);
                sb.Append(line);
            }
        }

        var chars = sb.ToString().ToCharArray();

However, you should add each line into a stringbuilder and then convert it into char array. The thing is what you really want to do?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.