0
foreach (string line in assigment_lines)
    {
        // Object reference not set to an instance of 
        chars.AddRange(line.Split('=')); object.
    }
   string[] strArray = chars.ToArray();

My program give me Null Reference Exception in above code what a problem ?

Jason Down
  • 20,773
  • 11
  • 80
  • 114
Muhammad Faisal
  • 844
  • 2
  • 9
  • 12
  • 2
    Either `chars` is null or one of the `line`s in `assignment_lines` is null. – Devin Burke Dec 24 '11 at 23:44
  • 5
    Welcome to the site - please write a proper title in order for people to better understand your questions at a glance. See http://meta.stackexchange.com/questions/18614/style-guide-for-questions-and-answers for more. – BoltClock Dec 24 '11 at 23:45
  • Too localized. As a developer, this sort of thing *must* be debugged/fixed by yourself. Look at the call-stack, reason what value might be null, and/ or use a debugger. In the code posted there are only two member invocations on the line, which is why Justin Satyr could easily point out which expressions *could* be null. –  Dec 24 '11 at 23:46
  • @BoltClock Not sure there is a "proper title" for this question :( –  Dec 24 '11 at 23:46
  • His previous question is directly related to this: http://stackoverflow.com/questions/8620629/string-array-in-c-sharp – Dykam Dec 24 '11 at 23:48
  • possible duplicate of [What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – John Saunders Dec 24 '11 at 23:54
  • Ok i change question title sorry for my noobness .. – Muhammad Faisal Dec 25 '11 at 00:16
  • Voted to close because "SO is not a debugger". – Cody Gray Dec 25 '11 at 02:42

1 Answers1

3

Make sure that line is not null also check where do you define chars array. You can do something like this:

foreach (string line in assigment_lines)
{
  if (!string.IsNullOrEmpty(line)) {  
      chars.AddRange(line.Split('=')); // Object reference not set to an instance of object.
  }
}
string[] strArray = chars.ToArray();
Tim
  • 2,375
  • 2
  • 28
  • 39
  • But one more question 4 u yesterday i run this program its running ok but when i run today it give me exception why ? – Muhammad Faisal Dec 24 '11 at 23:46
  • Perhaps you have used different data? Also could be that line.Split does return null if it can't split – Tim Dec 24 '11 at 23:48
  • @muhammad - its xmas eve so magical things tend to happen. Or maybe assignment_lines is different for reasons only you can figure – Robert Levy Dec 25 '11 at 00:11