1
string filename = o.FileName;
string[] textlines = File.ReadAllLines(filename);
string[] mainAndSublines = null;
int i = 0;

foreach(string textline in textlines)
{
   if (textline.Substring(0, 1) != " ")
   {
      i++;
   }

   mainAndSublines[i] = textlines[i];   //Getting error, NullReferenceException was not handled
}

I am getting thee error:

NullReferenceException was not handled

2
  • There are two types of lines in the text file, 1. starts without a blank space, 2. starts with a blank space, i am storing each of them in an array. example, First line i belong to firstline, Mark i belong to firstline, Joy Second line i belong to secondline, Mark i belong to secondline, Joy looking for, array[0]=> First line i belong to firstline, Mark i belong to firstline, Joy array[1]=> Second line i belong to secondline, Mark i belong to secondline, Joy Commented Nov 20, 2011 at 22:47
  • I don't understand. Perhaps you could update the question with the missing information instead of writing it in a comment? Then you could use formatting to make the description more clear and hopefully(!) it will make more sense. Commented Nov 20, 2011 at 22:57

4 Answers 4

4

You have initialized mainAndSublines to null.

If you know the desired size in advance use this:

string[] mainAndSublines = new string[100];

If you don't know the size you want in advance, use a dynamically resizing container such as a List:

List<string> mainAndSublines = new List<string>();
Sign up to request clarification or add additional context in comments.

4 Comments

List is the most suitable solution here
<pedant>Actually he has initializes mainAndSublines to a value, explicitly - the null value. It's still a value :)</pedant>
Jon: He has assigned value to variable, but hasn't initialized it as an array, has he?
@abatishchev: No, but Mark claimed "you haven't initialized mainAndSublines to any value" which isn't true. If he'd said, "you haven't created an array and assigned a value to mainAndSublines to refer to that array" that would have been a different matter :)
1

Simply because your array mainAndSublines is null and you can't access an element of an array that is null ...

Comments

1

You've actually got two problems here - one is that you're trying to set values into a null array, and the other is how you're trying to find whether the first character of the line is a space.

I would use a List<String> for the non-space-starting lines, as you can't tell how many there will be, but then you're assuming that every line will be non-empty. Fortunately it's easy to fix this using StartsWith.

I'd then use LINQ if you're using .NET 3.5 or higher, which makes the whole thing really simple:

List<string> mainAndSublines = File.ReadAllLines(filename)
                                   .Where(x => !x.StartsWith(" "))
                                   .ToList();

In .NET 4 you can make this more memory-efficient using File.ReadLines instead of File.ReadAllLines - this streams the file instead of loading the whole thing into memory to start with.

Comments

0

You need to initialize second array too, as you did with first, probably equal by size.

string[] mainAndSublines = new string[textlines.Length];

But the most suitable solution here will be Mark's one - auto-resizable container.

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.