INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Checking for null.......

Checking for null.......

(OP)

I've tried two ways to check for null and neither work:
I have a menu control and when I click a link the below event is fired. I tried two ways to check for null and neither work. Can someone please help?

protected void MenuItems_OnMenuItemClick(object sender, MenuEventArgs e)
{
try
{
if ( e.Item.Text != null)
{
Debug.WriteLine("MenuItem: " + e.Item.Text );
}
}
catch(Exception ex)
{
throw (ex);
}
}

And:

protected void MenuItems_OnMenuItemClick(object sender, MenuEventArgs e)
{
try
{
System.Web.UI.WebControls.MenuItem mi = new System.Web.UI.WebControls.MenuItem();

if (mi.Parent.Text != null)
{
Debug.WriteLine("MenuItem: " + e.Item.Text );
}
}
catch(Exception ex)
{
throw (ex);
}
}

RE: Checking for null.......

Have you tried with the built in string.isNullOrEmpty?

if (!string.IsNullOrEmpty(mi.Parent.Text))
{
code
}

RE: Checking for null.......

(OP)
Yes, I just tried that and then I tried if(!string.IsNullOrEmpty(mi.Parent.Text.ToString())) and if (!string.IsNullOrEmpty(e.Item.Parent.Parent.Text)) which didn't work either. I appreciate your attempt, Moregelen. Do you or anyone else have any other ideas?

RE: Checking for null.......

If that is saying it isn't null, then it most likely isn't null. Have you run it in debugging mode to verify that it really is a NULL?

RE: Checking for null.......

(OP)
Here is what I get using debug and then in the exception:

?e.Item.Parent.Parent.Text
'e.Item.Parent' is null
Step into: Stepping over non-user code 'System.Web.UI.WebControls.MenuEventArgs.Item.get'
A first chance exception of type 'System.NullReferenceException' occurred in App_Web_fjsl66dx.dll
A first chance exception of type 'System.NullReferenceException' occurred in App_Web_fjsl66dx.dll

RE: Checking for null.......

Oh. Your issue isn't that the text isn't null, its that the parent is null.

Try with:

if (e.Item.Parent != null && !string.IsNullOrEmpty(e.Item.Parent.Text))
{
code
}

RE: Checking for null.......

Or whatever it is. Make sure the top level elements aren't null.

RE: Checking for null.......

(OP)
That did it! Thanks so much. I've been struggling with null values in C# all day. Part of the learning process.... Thanks again!.

RE: Checking for null.......

Yup. Just remember that if its possible for an object higher up to be null, you need to check that first. Doing e.Item.Parent.Text is going ahead and making the assumption that Parent isn't null; so when it is null, things crash and burn.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close