当前位置: 动力学知识库 > 问答 > 编程问答 >

c# 4.0 - Always getting error-"Object reference not set to an instance of an object."

问题描述:

I tried this way...so that when i click any menuitem in button1 it will check and check the same menuitem in the second button...but some where am getting error...I want to write it in better way .so that it will work as i need with out any issuse.here is the below code i did so far...how can i solve this issue as well as how can i improve this coding...

here is my code..

 <Grid>

<Grid.RowDefinitions>

<RowDefinition Height="50"/>

<RowDefinition Height="50"/>

<RowDefinition Height="50"/>

<RowDefinition Height="50"/>

</Grid.RowDefinitions>

<StackPanel Grid.Row="0" Orientation="Horizontal" FlowDirection="LeftToRight">

<Button Name="btnPhone" Content="Business" Click="OnbtnPhoneClick" ></Button>

<Button Name="ddBtnPhone" Width="25" Click="OnddBtnPhoneClick" Height="47">

<Button.Content>

<Path x:Name="btnArrow1" Margin="4" VerticalAlignment="Center" Width="10" Fill="#FF527DB5" Stretch="Uniform" HorizontalAlignment="Right" Data="F1 M 301.14,-189.041L 311.57,-189.041L 306.355,-182.942L 301.14,-189.041 Z "/>

</Button.Content>

<Button.ContextMenu>

<ContextMenu x:Name="cmPhone">

<MenuItem Header="Assistant " Name="mnIAssistant" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/> <MenuItem Header="Business" Name="mnIBusiness" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/> <MenuItem Header="Business 2 " Name="mnIBusiness2" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/>

<MenuItem Header="Business FAX " Name="mnIBusinessFAX" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/>

<MenuItem Header="Call Back" Name="mnICallback" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/>

<MenuItem Header="Car" Name="mnICar" Click="OnMenuItemChecked"/>

</ContextMenu>

</Button.ContextMenu>

</Button>

<TextBox Height="19" Name="textBox1" Width="120" />

</StackPanel>

<StackPanel Grid.Row="2" Orientation="Horizontal" FlowDirection="LeftToRight">

<Button Name="btnHome" Content="Home" Click="OnbtnHomeClick" Height="26" Width="64" />

<Button Name="ddBtnHome" Width="25" Click="OnddBtnHomeClick" Margin="0,0,0,5" >

<Button.Content>

<Path x:Name="btnArrow2" Margin="4" VerticalAlignment="Center" Width="10" Fill="#FF527DB5" Stretch="Uniform" HorizontalAlignment="Right" Data="F1 M 301.14,-189.041L 311.57,-189.041L 306.355,-182.942L 301.14,-189.041 Z "/>

</Button.Content>

<Button.ContextMenu>

<ContextMenu Name="cmHome">

<MenuItem Header="Assistant " Name="mnIAssistant1" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/>

<MenuItem Header="Business" Name="mnIBusiness1" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/>

<MenuItem Header="Business 2 " Name="mnIBusiness21" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/>

<MenuItem Header="Business FAX " Name="mnIBusinessFAX1" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/>

<MenuItem Header="Call Back" Name="mnICallback1" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/>

<MenuItem Header="Car" Name="mnICar1" Click="OnMenuItemChecked"/>

</ContextMenu>

</Button.ContextMenu>

</Button>

<TextBox Height="23" Name="textBox2" Width="120" />

</StackPanel>

<Border Grid.Row="3" BorderThickness="2" BorderBrush="Blue">

<TextBlock Name="tbWebpage" Grid.Row="3" Background="White" VerticalAlignment="Top" Height="20">

<Hyperlink NavigateUri="tbWebpage.Text" RequestNavigate="Hyperlink_RequestNavigate"></Hyperlink>

</TextBlock>

</Border>

</Grid>

code behind xaml

 namespace MenuItemsChecked

{

public partial class MainWindow : Window

{

//string[] arrMenitems = { "business", "a1", "a2", "a3","a4" };

Button btnCommon = new Button();

TextBox txtCommon = new TextBox();

MenuItem mnItem;

public MainWindow()

{

InitializeComponent();

}

private void AddMenuItems(ContextMenu cMenu, Button btnName, TextBox txtBoxName)

{

//mnItem.Items.Add(arrMenitems);

if (cMenu.Items.Count != 0)

{

//mnItem = new MenuItem();

//mnItem.Header = "Business";

//mnItem.IsCheckable = true;

//cMenu.Items.Add(mnItem);

//mnItem = new MenuItem();

//mnItem.Header = "Assistant";

//mnItem.IsCheckable = true;

//cMenu.Items.Add(mnItem);

//mnItem = new MenuItem();

//mnItem.Header = "Business 2";

//mnItem.IsCheckable = true;

//cMenu.Items.Add(mnItem);

//mnItem = new MenuItem();

//mnItem.Header = "Business FAX";

//mnItem.IsCheckable = true;

//cMenu.Items.Add(mnItem);

//mnItem = new MenuItem();

//mnItem.Header = "Call Back";

//mnItem.IsCheckable = true;

//cMenu.Items.Add(mnItem);

//cMenu.Items.Add(mnItem);

//cmCommon = cMenu;

cMenu.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(OnMenuItemClick));

cMenu.AddHandler(MenuItem.MouseLeftButtonUpEvent, new MouseButtonEventHandler(OnMouseButtonUpEventClick));

// cMenu.AddHandler(MenuItem.CheckedEvent, new RoutedEventHandler(OnMenuItemChecked));

btnCommon = btnName;

//txtCommon = txtBoxName;

}

}

private void OnMenuItemChecked(object sender, RoutedEventArgs e)

{

RoutedEventArgs args = e as RoutedEventArgs;

MenuItem item = args.OriginalSource as MenuItem;

if (sender == mnIAssistant)

{

mnIAssistant.IsChecked = true;

mnIAssistant1.IsChecked = true;

}

if (sender == mnIBusiness)

{

mnIBusiness.IsChecked = true;

mnIBusiness1.IsChecked = true;

}

if (sender == mnIBusiness2)

{

mnIBusiness2.IsChecked = true;

mnIBusiness21.IsChecked = true;

}

if (sender == mnIBusinessFAX)

{

mnIBusinessFAX.IsChecked = true;

mnIBusinessFAX1.IsChecked = true;

}

if (sender == mnICallback)

{

mnICallback.IsChecked = true;

mnICallback1.IsChecked = true;

}

if (sender == mnICar)

{

mnICar.IsChecked = true;

mnICar1.IsChecked = true;

}

if (sender == mnIAssistant1)

{

mnIAssistant.IsChecked = true;

mnIAssistant1.IsChecked = true;

}

if (sender == mnIBusiness1)

{

mnIBusiness.IsChecked = true;

mnIBusiness1.IsChecked = true;

}

if (sender == mnIBusiness21)

{

mnIBusiness2.IsChecked = true;

mnIBusiness21.IsChecked = true;

}

if (sender == mnIBusinessFAX1)

{

mnIBusinessFAX.IsChecked = true;

mnIBusinessFAX1.IsChecked = true;

}

if (sender == mnICallback1)

{

mnICallback.IsChecked = true;

mnICallback1.IsChecked = true;

}

if (sender == mnICar1)

{

mnICar.IsChecked = true;

mnICar1.IsChecked = true;

}

}

private void OnMouseButtonUpEventClick(object sender, MouseButtonEventArgs e)

{

(sender as MenuItem).IsChecked = !(sender as MenuItem).IsChecked;

((sender as MenuItem).Parent as ContextMenu).IsOpen = false;

}

private void OnMenuItemClick(object sender, RoutedEventArgs e)

{

RoutedEventArgs args = e as RoutedEventArgs;

// MenuItem item = args.OriginalSource as MenuItem;

//string header = item.Header.ToString();

MenuItem menuitem = sender as MenuItem; ///always getting error-"Object reference not set to an instance of an object."

//ContextMenu cm = sender as ContextMenu;

string name = menuitem.Name;

string header = menuitem.Header.ToString();

if ((sender == mnIBusiness) |(sender == mnIBusiness1))

{

if (header == "Business")

{

mnIBusiness.IsChecked = true;

mnIBusiness1.IsChecked = true;

}

btnCommon.Content = header;

//item.IsCheckable = true;

}

else if ((sender == mnIAssistant) | (sender == mnIAssistant1))

{

if (header == "Assistant")

{

mnIAssistant.IsChecked = true;

mnIAssistant1.IsChecked = true;

}

btnCommon.Content = header;

///item.IsChecked = true;

}

else if ((sender == mnIBusiness2) | (sender == mnIBusiness21))

{

//if(header=="Business 2")

//{

mnIBusiness2.IsChecked = true;

mnIBusiness21.IsChecked = true;

//}

btnCommon.Content = header;

//item.IsChecked = true;

}

else if ((sender == mnIBusinessFAX) |(sender == mnIBusinessFAX1))

{

if(header=="Business FAX")

{

mnIBusinessFAX.IsChecked = true;

mnIBusinessFAX1.IsChecked = true;

}

btnCommon.Content = header;

//item.IsChecked = true;

}

else if ((sender == mnICallback1) | (sender == mnICallback))

{

if (header == "Call Back")

{

mnICallback.IsChecked = true;

mnICallback1.IsChecked = true;

}

btnCommon.Content = header;

//item.IsChecked = true;

}

else

{

if (header == "Car")

{

mnICar.IsChecked = true;

mnICar1.IsChecked = true;

}

btnCommon.Content = header;

}

txtCommon.Text = string.Empty;

}

private void OnddBtnPhoneClick(object sender, RoutedEventArgs e)

{

ButtonContextMenuDisplay(sender);

AddMenuItems(cmPhone, btnPhone, textBox1);

}

private void OnbtnPhoneClick(object sender, RoutedEventArgs e)

{

}

private void OnbtnHomeClick(object sender, RoutedEventArgs e)

{

}

private void OnddBtnHomeClick(object sender, RoutedEventArgs e)

{

ButtonContextMenuDisplay(sender);

AddMenuItems(cmHome, btnHome, textBox2);

}

private void ButtonContextMenuDisplay(object sender)

{

(sender as Button).ContextMenu.IsEnabled = true;

(sender as Button).ContextMenu.PlacementTarget = (sender as Button);

(sender as Button).ContextMenu.IsOpen = true;

(sender as Button).ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;

}

}

}

网友答案:

hey kida somehow your onmenuitemclick is being called more than one time and it is working well for first time but when it is called again then menuitem becomes null so it giving you nullreference exception i have put a small check..to make it work..so check it out..

 private void OnMenuItemClick(object sender, RoutedEventArgs e)
    {
        RoutedEventArgs args = e as RoutedEventArgs;
        // MenuItem item = args.OriginalSource as MenuItem;
        //string header = item.Header.ToString();
        MenuItem menuitem = sender as MenuItem; ///always getting error-"Object reference not set to an instance of an object."

        if (menuitem != null)
        {
            ContextMenu cm = sender as ContextMenu;
            //    string name = menuitem.Name;
            string header = menuitem.Header.ToString();
            if ((sender == mnIBusiness) | (sender == mnIBusiness1))
            {
                if (header == "Business")
                {

                    mnIBusiness.IsChecked = true;
                    mnIBusiness1.IsChecked = true;
                }
                btnCommon.Content = header;
                //item.IsCheckable = true;                

            }
            else if ((sender == mnIAssistant) | (sender == mnIAssistant1))
            {
                if (header == "Assistant")
                {
                    mnIAssistant.IsChecked = true;
                    mnIAssistant1.IsChecked = true;
                }
                btnCommon.Content = header;
                ///item.IsChecked = true;
            }
            else if ((sender == mnIBusiness2) | (sender == mnIBusiness21))
            {
                //if(header=="Business 2")
                //{
                mnIBusiness2.IsChecked = true;
                mnIBusiness21.IsChecked = true;
                //}
                btnCommon.Content = header;
                //item.IsChecked = true;
            }
            else if ((sender == mnIBusinessFAX) | (sender == mnIBusinessFAX1))
            {
                if (header == "Business FAX")
                {
                    mnIBusinessFAX.IsChecked = true;
                    mnIBusinessFAX1.IsChecked = true;
                }
                btnCommon.Content = header;
                //item.IsChecked = true;
            }
            else if ((sender == mnICallback1) | (sender == mnICallback))
            {
                if (header == "Call Back")
                {
                    mnICallback.IsChecked = true;
                    mnICallback1.IsChecked = true;
                }
                btnCommon.Content = header;
                //item.IsChecked = true;
            }
            else
            {
                if (header == "Car")
                {
                    mnICar.IsChecked = true;
                    mnICar1.IsChecked = true;
                }
                btnCommon.Content = header;

            }

            txtCommon.Text = string.Empty;
        }
    }

post your query in comments..

分享给朋友:
您可能感兴趣的文章:
随机阅读: