how to handle NullReferenceException


  •  Hi i'm trying to execute the fallowing code but it shows NullRefereneException can anybody know the solution for this

    this is my xaml.cs file

    using System;
    using System.IO;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Navigation;

    namespace Test
     public partial class Scene1
      public Scene1()

      private void image(object sender, System.Windows.RoutedEventArgs e)
       RichTextEditor.AppendText ("Stroke method executed");

      private void selectShape(object sender,  System.Windows.Controls.SelectionChangedEventArgs e)
                RichTextEditor.AppendText ("Combobox method executed");



    and this is my .xaml file

     Width="640" Height="480">

      <Storyboard x:Key="OnLoaded"/>

      <EventTrigger RoutedEvent="FrameworkElement.Loaded">
       <BeginStoryboard x:Name="OnLoaded_BeginStoryboard" Storyboard="{DynamicResource OnLoaded}"/>
     <Canvas HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="1.00000000000011,-0.999999999999915,0,-2.00000000000006" Width="Auto" Height="Auto" x:Name="Canvas" RenderTransformOrigin="0.5,0.5" Grid.ColumnSpan="1" OpacityMask="sc#1, 0.244989648, 0.213771492, 0.8823977">
      <TabControl Width="630" Height="88" Canvas.Left="2.00000000000032" Canvas.Top="0" x:Name="TabControl" TabStripPlacement="Top">
       <TabItem x:Name="Insert" Header="Insert">
         <Button HorizontalAlignment="Left" VerticalAlignment="Top" Width="70" Height="25" Content="Text" VerticalContentAlignment="Center"/>
       <TabItem x:Name="Edit" Header="Edit">
         <Button HorizontalAlignment="Left" VerticalAlignment="Top" Width="70" Height="25" VerticalContentAlignment="Center" Content="Cut"/>
         <Button HorizontalAlignment="Left" VerticalAlignment="Top" Width="70" Height="25" Content="Copy" VerticalContentAlignment="Center" />
         <Button HorizontalAlignment="Left" VerticalAlignment="Top" Width="70" Height="25" Content="Paste" VerticalContentAlignment="Center"/>
       <TabItem x:Name="Drawing" Header="Drawing">
         <Label Content ="Shape :"/>
         <ComboBox x:Name ="shapes" Height ="25" Width ="100" SelectionChanged="selectShape">
          <ComboBoxItem Content="Line"/>
          <ComboBoxItem IsSelected ="True" Content="Circle"/>
          <ComboBoxItem Content="Rectangle"/>
          <ComboBoxItem Content="Ovel"/>
         <Button HorizontalAlignment="Left" VerticalAlignment="Top" Width="70" Height="25" Content="Stroke" VerticalContentAlignment="Center" Click="image"/>
      <RichTextBox Grid.Column="0" Grid.Row="0" x:Name="RichTextEditor" AcceptsReturn="True" Background="sc#1, 0.941325068, 0.940964162, 0.940411448" d:LayoutOverrides="VerticalAlignment" Margin="3.00000000000773,89.9420289855063,0,0" Grid.ColumnSpan="1" VerticalAlignment="Top" Height="358.513513513508" HorizontalAlignment="Left" Width="630" Canvas.Left="2" Canvas.Top="35.9999999999998" RenderTransformOrigin="0,0" ToolTip="U Can Drag and drop here" AcceptsTab="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
        <TranslateTransform X="0" Y="0"/>
        <ScaleTransform ScaleX="1" ScaleY="1.0724637681159421"/>
        <SkewTransform AngleX="0" AngleY="0"/>
        <RotateTransform Angle="0"/>
        <TranslateTransform X="0" Y="0"/>
        <TranslateTransform X="0" Y="0"/>
              <FlowDocument AllowDrop="True" FontSize="11" FontFamily="TimesNewRoman" TextAlignment="Left">


    thanx - Sai

    Monday, May 08, 2006 7:36 PM


  • Ya i got solution i just remove the default selection of combobox item then its for fine

    thanx - Sai


    Tuesday, May 09, 2006 7:45 AM

All replies

  • To help you out, we would need details for the NullRefException(stack trace, ...) as well as any inner exception details...
    Monday, May 08, 2006 8:45 PM
  • hi,

    this is the inner details of the exception:

    "Object Reference not set to an instance of an object"

    and i get this exception when i use it for ComboBox,

    when i use the same for Button it is not raising the exception.

    why is it so?



    Tuesday, May 09, 2006 5:13 AM
  • Give a try to this:

    private void selectShape(object sender,  RoutedEventArgs e)
                RichTextEditor.AppendText ("Combobox method executed");

    Tuesday, May 09, 2006 7:23 AM
  • Ya i got solution i just remove the default selection of combobox item then its for fine

    thanx - Sai


    Tuesday, May 09, 2006 7:45 AM