NullReferenceException was unhandled - c#

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!

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;
}

Related

How to handle NoNullAllowedException in try/catch?

I have a form in C# for inputting some data into a List.
The form consists of text boxes and up and down numeric boxes. Everything works fine but I want to have an error handler (try/catch) in my code so it will check if any of the text boxes are empty or the numeric boxes are left to 0, if thats the case it should pop up an error message.
I tried :
try
{
//code
}
catch (NoNullAllowedException e) //tried it without the e as well
{
//code
}
The code I was having in the brackets its the following one. Sometimes the GetItemDetails() was throwing me an error saying that not all code paths returns a value.
Any thoughts why is doing this or how can I fix it?
public iRepairable GetItemDetails()
{
Shirt shirt = null;
TShirt tshirt = null;
Trouser trouser = null;
Shoe shoe = null;
Boolean isShirt = true;
Boolean isTshirt = true;
Boolean isTrouser = true;
Boolean isShoe = true;
if (rdoShirt.Checked == true)
{
shirt = new Shirt(txtBrand.Text, Convert.ToDouble(txtPrice.Text), Convert.ToInt32(txtAmount.Text), txtCollection.Text);
isTshirt = false;
isTrouser = false;
isShoe = false;
}
else if (rdoTShirt.Checked == true)
{
tshirt = new TShirt(txtBrand.Text, Convert.ToDouble(txtPrice.Text), Convert.ToInt32(txtAmount.Text), txtCollection.Text);
isShirt = false;
isTrouser = false;
isShoe = false;
}
else if (rdoTrouser.Checked == true)
{
trouser = new Trouser(txtBrand.Text, Convert.ToDouble(txtPrice.Text), Convert.ToInt32(txtAmount.Text), txtCollection.Text);
isShirt = false;
isTshirt = false;
isShoe = false;
}
else
{
shoe = new Shoe(txtBrand.Text, Convert.ToDouble(txtPrice.Text), Convert.ToInt32(txtAmount.Text), txtCollection.Text);
isShirt = false;
isTrouser = false;
isTshirt = false;
}
if (isShirt)
{
return shirt;
}
else if (isTshirt)
{
return tshirt;
}
else if (isTrouser)
{
return trouser;
}
else //if(isShoe)
{
return shoe;
}
First of all, the NoNullAllowedException is not for list or just null values. Is the exception that throws when you want to insert null values in a column that doesn't allow them (for more info, MSDN).
For your code, place at the bottom of your code a default return value (but as far as i can see, your code shouldnt break at all)

If Session match string

I got a little problem. I got a if statement which says if Session isn't equal 3, then do something, and if that isn't true, then do something else. My problem is just, that it isn't working proberly.
I've already tried:
1)
if (Session["userrank"] != "3")
{
pnlAdmin.Visible = false;
}
else
{
pnlAdmin.Visible = true;
}
2)
if (Session["userrank"].ToString() != "3")
{
pnlAdmin.Visible = false;
}
else
{
pnlAdmin.Visible = true;
}
3)
if ((string)Session["userrank"] != "3")
{
pnlAdmin.Visible = false;
}
else
{
pnlAdmin.Visible = true;
}
4)
if (((string)Session["userrank"]) != "3")
{
pnlAdmin.Visible = false;
}
else
{
pnlAdmin.Visible = true;
}
but none of them seems to work. And i have already checked if there's a Session called userrank that is getting the result 3.
sorry for the "stupid" question. I'm kind of new to C# & ASP.net.
Best Regards,
Anton
Your code sets pnlAdmin.Visible = false; if whatever is in Session["userrank"] is not 3.
It sets pnlAdmin.Visible = true; if whatever is in Session["userrank"] is 3.
You said it is 3; therefore, the panel should be visible. And that seems to be what is happening.

How do i Check if a xml Sibling has attributes?

My program is doing weird stuff if i delete one of my Attributes because it is not able to handle Siblings without Attributes. Now i googled for a while but i am not able to find a good way to check for attributes.
Whats the way you prefer checking for attributes?
while (FXMLNode != null)
{
if (FXMLNode.Name.ToLower() == "datei")
{
xmlInformationen oInfo = new xmlInformationen();
oInfo.Dateipfad = FXMLNode.InnerText;
if (FXMLNode.Attributes["checked"].Value.ToString() == "true")
oInfo.CheckBox = true;
else if (FXMLNode.Attributes["checked"].Value.ToString() == "false")
oInfo.CheckBox = false;
else if(FXMLNode == null)
oInfo.CheckBox = true;
else
oInfo.CheckBox = true;
lstAttribute.Add(oInfo);
iCounter++;
if (FXMLNode.NextSibling == null)
{
FXMLNode = FXMLNode.FirstChild;
}
else
{
FXMLNode = FXMLNode.NextSibling;
}
}
else
{
if (FXMLNode.NextSibling == null)
{
FXMLNode = FXMLNode.FirstChild;
}
else
{
FXMLNode = FXMLNode.NextSibling;
}
}
}
You are accessing the value of an attribute without knowing if the attribute exists or not. Rewrite your code to check for the attribute first:
oInfo.CheckBox = true;
if(FXMLNode == null) oInfo.CheckBox = true; //not sure why you set it to true here
else if (FXMLNode.HasAttribute("checked"))
{
if (FXMLNode.Attributes["checked"].Value.ToString() == "true")
oInfo.CheckBox = true;
else if (FXMLNode.Attributes["checked"].Value.ToString() == "false")
oInfo.CheckBox = false;
}
Please note that checking if the Xml element is null should be the first thing you do. If it's null then it surely won't have any attributes but you'll have an exception.
you can just check thats the attribute in not null like this
if (FXMLNode.Attributes["checked"]!=null)
and then check the value

Insert value into radiobutton

I'm trying to insert a value into a RadioButton based on a value I have in an array.
This is what I'm trying to acheive, but I can't seem to get it work.
I know the IsChecked is a check if the RadioButton is checked or not, but I want to describe the means.
if (arrAnswer[nAnswerNum] == 0)
{
radioTrue.IsChecked = false;
radioFalse.IsChecked = false;
}
else if (arrAnswer[nAnswerNum] == 1)
{
radioTrue.IsChecked = true;
}
else if (arrAnswer[nAnswerNum] == 2)
{
radioFalse.IsChecked = true;
}
Thanks
Do it like this, it's much simpler
radioTrue.IsChecked = arrAnswer[nAnswerNum] == 1;
radioFalse.IsChecked = arrAnswer[nAnswerNum] == 2;
also dont forget to check if the index does exists to prevent indexoutofrange exception

How does C# evaluate Logic block?

I have the following line of code:
public bool dcpl_radar()
{
if (radar == null)
return false;
else
{
if (radar != null)
{
if (radar.InvokeRequired)
radar.BeginInvoke(new MethodInvoker(delegate()
{
radar.Visible = false;
}));
else
this.radar.Visible = false;
radar = null;
}
return true;
}//end of else statement
}
but VStudio keeps throwing an error on the invoke line. I've checked the Debugger and if (radar == null) is true, yet VStudio is trying to evaluate a part of the code it shouldn't be in. Can someone explain why it's doing this please?
Wait a minute... I think we have a race condition.
Lets say you BeginInvoke, almost immediately you set radar = null.
There really is no telling when your anonymous delegate will be executed.
I would imagine this should solve your issue.
public bool dcpl_radar()
{
if (radar != null)
{
if (radar.InvokeRequired)
{
radar.BeginInvoke(new MethodInvoker(HideRadar));
}
else
{
HideRadar();
}
return true;
}
return false;
}
private void HideRadar()
{
this.radar.Visible = false;
this.radar = null;
}
What is happening:
The anonymous delegate is being called after you set the radar to null.
How to fix it
public bool dcpl_radar()
{
if (radar == null)
return false;
else
{
if (radar != null)
{
if (radar.InvokeRequired)
radar.BeginInvoke(new MethodInvoker(delegate()
{
radar.Visible = false;
radar = null;
}));
else {
this.radar.Visible = false;
radar = null;
}
}
return true;
}//end of else statement
}
(Note where I've moved your 'null' assignments).
Though I am a bit worried about the point of setting a variable to null, it's generally a sign of a bad design.

Categories