1

I'm just getting the hang of Lightswitch, but I keep getting the null reference exception error when I'm trying to find out if a selected item in a datagrid contains the letters "CMP". I look all over the place but I think I'm doing something wrong. Here is my code for reference:

if(string.IsNullOrWhiteSpace(this.location.SelectedItem.locationID))
        {
            this.ShowMessageBox("test"); //not sure what to put there so I just made something up
        }
        else if (this.location.SelectedItem.locationID.Contains("CMP"))
        {
            this.FindControl("datePurchased").IsVisible = true;
            this.FindControl("age").IsVisible = true;
            this.FindControl("userList").IsVisible = true;
        }
        else
        {
                this.FindControl("datePurchased").IsVisible = false;
                this.FindControl("age").IsVisible = false;
                this.FindControl("userList").IsVisible = false;
        }

I also tried

if(this.location.selecteditem.locationID != null)

if(string.IsNullOrEmpty)

but it always throws me the same error. Any help would be much appreciated!

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
jester113
  • 13
  • 2
  • 1
    probably your this.location.selecteditem variable or this.location is null – Sercan Ozdemir Apr 28 '14 at 05:43
  • its a cell in a datagrid though, and there are some values there already. When I debugged it, it shows that there is a value, then all of a sudden it said its null. – jester113 Apr 28 '14 at 05:44
  • i dont know how lightswitch works, but in asp.net if you select an item and dont keep it on postbacks, you cant reach them back, i think theres something like that in here. – Sercan Ozdemir Apr 28 '14 at 05:46
  • sorry I'm still learning the ropes of programming...what do you mean by that? can you give me an example? – jester113 Apr 28 '14 at 05:49
  • as i said i dont know lightswitch but, please read [here](http://stackoverflow.com/questions/18406795/dropdownlist-not-keeping-the-selected-item-after-postback) – Sercan Ozdemir Apr 28 '14 at 05:52
  • What do you mean by "it said its null"? What said that *what* is null? What do you mean by "there is a value" - there is a value for *what*, exactly? What do you expect to happen if nothing is selected? – Jon Skeet Apr 28 '14 at 05:53
  • 1
    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) – nvoigt Apr 28 '14 at 05:56
  • when i hover over the selecteditem part when i set a breakpoint, i get the popup saying "this.location.selecteditem | null" – jester113 Apr 28 '14 at 05:58
  • hi nvoigt, yup i tried those. I tried != null = null == null and other checks stated on that post. Still throws me that exception. – jester113 Apr 28 '14 at 05:59

1 Answers1

2

I guss this.location or this.location.selecteditem may be null, So you got that error. So please try this if condition instead of your if condition way

if(this.location != null && this.location.selecteditem !=null && this.location.selecteditem.locationID != null)
{
       //Write your code here 
}

So your final code look like

        if(this.location == null && this.location.selecteditem ==null && this.location.selecteditem.locationID == null)
        {
          this.ShowMessageBox("test"); //not sure what to put there so I just made something up
        }    

        else if (this.location.SelectedItem.locationID.Contains("CMP"))
        {
            this.FindControl("datePurchased").IsVisible = true;
            this.FindControl("age").IsVisible = true;
            this.FindControl("userList").IsVisible = true;
        }
        else
        {
                this.FindControl("datePurchased").IsVisible = false;
                this.FindControl("age").IsVisible = false;
                this.FindControl("userList").IsVisible = false;
        }
  • awesome! it stopped throwing the exception...but now the code inside doesnt fire >.> but at least the exception is gone ^o^ – jester113 Apr 28 '14 at 06:06
  • Cool! I have updated my answer. Please take a look that, and please use break point for which values are threw null. –  Apr 28 '14 at 06:09
  • 1
    Thios is perfect its working now! thanks so much! I put the code in its own method and called it everytime the selection was changed and its works now! thanks so much! – jester113 Apr 28 '14 at 06:12