Object reference not set to an instance of an object.

4005
2
12-02-2013 06:52 AM
srikanthpilli
New Contributor II
Hello,

I am performing an attribute query, and for that particular attribute - we want to zoom in to that  feature and draw a graphics layer.

But although I added the graphic layer in the Mainpage.xaml - I am getting the following error at this particular line of code  :



graphicsLayer1.Graphics.Add(selectedFeature);

Error : Object reference not set to an instance of an object.




Mainpage.xaml
--------------



<esri:Map x:Name="Map" MouseClick="Map_MouseClick" Progress="MyMap_Progress" Background="White" Extent="-8958052,4925394,-7964275,5744546" ExtentChanged="MyMap_ExtentChanged" IsLogoVisible="False"  >
                        <esri:ArcGISTiledMapServiceLayer x:Name="esriBasemap" ID="BaseLayer" Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" Opacity="1" Visible="True" Initialized="ArcGISTiledMapServiceLayer_Initialized" />

                        <esri:GraphicsLayer ID=" MyGraphicsLayer1" />
                 
                    </esri:Map>



Mainpage.xaml.cs
-----------------


  private void btnFind_Click(object sender, RoutedEventArgs e)
        {
            QueryTask queryTask= new QueryTask("http://" + CurrentPage.ServerName + "/ArcGIS/rest/services/DataViewer/MapServer/0");
            queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
            ESRI.ArcGIS.Client.Tasks.Query query = new ESRI.ArcGIS.Client.Tasks.Query();
       
            //query.Where = "CROSSINGNU ='" + txtUS.Text.ToString() + "'";
            query.Where = "CROSSINGNU ='" + txtUS.Text.ToString() + "'";
           
            query.ReturnGeometry = true;
            query.OutSpatialReference = Map.SpatialReference;
            query.OutFields.Add("*");

            queryTask.ExecuteAsync(query);           
        }

  private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
        {
          
            GraphicsLayer graphicsLayer1 = Map.Layers["MyGraphicsLayer1"] as GraphicsLayer;

       
          

            //graphicsLayer1.ClearGraphics();

            FeatureSet featureSet2 = args.FeatureSet;

            if (featureSet2 != null && featureSet2.Features.Count > 0)
            {
                Graphic selectedFeature = featureSet2.Features[0];
                // Highlight selected feature
                selectedFeature.Symbol = LayoutRoot.Resources["HighlightedMarkerSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
                graphicsLayer1.Graphics.Add(selectedFeature);
                ESRI.ArcGIS.Client.Geometry.Envelope selectedFeatureExtent = selectedFeature.Geometry.Extent;
                ESRI.ArcGIS.Client.Geometry.Envelope displayExtent = new ESRI.ArcGIS.Client.Geometry.Envelope(selectedFeatureExtent.XMin - 500, selectedFeatureExtent.YMin - 500, selectedFeatureExtent.XMax + 500, selectedFeatureExtent.YMax + 500);
                Map.ZoomTo(displayExtent);

            }

        }


Thanks for looking at it and providing your inputs.
0 Kudos
2 Replies
DenisT
by
New Contributor III
<esri:GraphicsLayer ID=" MyGraphicsLayer1" />
          
GraphicsLayer graphicsLayer1 = Map.Layers["MyGraphicsLayer1"] as GraphicsLayer;


The layer id starts with a white space in XAML.
0 Kudos
srikanthpilli
New Contributor II
Thanks a lot Denis for your help.

I really appreciate for taking time and going so much in detailed.

It worked
0 Kudos