3

I am using this code:

SetContentView (Resource.Layout.Results);
// Create your application here
string[] listResults = Intent.GetStringArrayExtra ("resultData");
LinearLayout linear = FindViewById<LinearLayout> (Resource.Id.linear);
TextView resultsPaste = FindViewById<TextView> (Resource.Id.resultsPaste);

foreach(string item in listResults) {
    resultsPaste.Text += item;
};

And I am receiving Object reference not set to an instance of an object next to the foreach. I have researched and can't find anything on it.

Why am I receiving the error?

jww
  • 97,681
  • 90
  • 411
  • 885
TechnoM4ncer
  • 51
  • 1
  • 1
  • 6
  • 2
    That would happen if `listResults` is null. Perhaps `Intent.GetStringArrayExtra` isn't working as you expect? – Andrew Oct 14 '14 at 01:15
  • 4
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ilian Oct 14 '14 at 01:27

1 Answers1

3

listResults, resultsPaste or both are null. Put a breakpoint before foreach and check the variables.

Miha Markic
  • 3,158
  • 22
  • 28
  • Helped me out. So simple, yet so easy to stumble over something small like that. – Jordec Aug 06 '15 at 18:28
  • Yes data is sometimes null in the wild. What is your recommended fix? Would an if statement to check for nulls be advised here? – Urasquirrel Aug 16 '18 at 22:21