GeoProcessing Service Error

1649
1
07-08-2013 11:17 AM
ChrisSmith14
New Contributor II
I just upgraded our custom Silverlight application from ArcGIS Silverlight API 2.4 to 3.1.  For some reason, one of our geoprocessing tasks is returning a ArgumentNullException, indicating that Parameter name: SpatialReference is null when the async task is executed.  I stepped through the code and saw that the spatialreference for one of the parameters was missing, so I assigned it in code but I'm still getting the error.  Any ideas?  My code is below:


private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (graphicsLayerPoly.Graphics.Count == 0)
            {
                MessageBox.Show("Please click on map to define Observer location.", "Viewshed", MessageBoxButton.OK);
                return;
            }
            LayoutRoot.Cursor = System.Windows.Input.Cursors.Wait;
            MapControl.Cursor = Cursors.Wait;

            MainPage mainPage = Application.Current.RootVisual as MainPage;
            string mapUnits = mainPage.scaleBar.MapUnit.ToString();

            List<GPParameter> parameters = new List<GPParameter>();

            double h = Convert.ToDouble(txtHeight.Text);
            if (cboUnitsH.SelectedItem.ToString().ToUpper() == "FEET" && zUnit.ToUpper() == "METERS") //input FEET to METERS
            {
                h = h * 0.3048;
            }
            else if ((cboUnitsH.SelectedItem.ToString().ToUpper() == "METERS" && zUnit.ToUpper() == "FEET")) //input METERS to FEET
            {
                h = h / 0.3048;
            }
            GPFeatureRecordSetLayer gpInputPoint = new GPFeatureRecordSetLayer("InputPoint", graphicsLayerPt.Graphics.First().Geometry);

            gpInputPoint.FeatureSet.Features.First().Attributes.Add("SPOT", (Convert.ToDouble(h) + Convert.ToDouble(txtHeight.Tag)).ToString());

            gpInputPoint.FeatureSet.Features.First().Attributes.Add("OFFSETA", 0);
            parameters.Add(gpInputPoint);

            GPFeatureRecordSetLayer gpInputArea = new GPFeatureRecordSetLayer("InputPolygon", graphicsLayerPoly.Graphics.First().Geometry);

            gpInputArea.FeatureSet.SpatialReference = gpInputPoint.FeatureSet.SpatialReference;  //here's where I assigned the spatialreference

            parameters.Add(gpInputArea);
            double r = Convert.ToDouble(txtRadius.Text);
            switch (cboUnitsR.SelectedItem.ToString().ToUpper())
            {
                case "FEET":
                    parameters.Add(new GPLinearUnit("Distance", esriUnits.esriFeet, r));
                    if (xyUnits.ToUpper() == "METERS") r = r * 0.3048; //convert to meters
                    break;
                case "METERS":
                    parameters.Add(new GPLinearUnit("Distance", esriUnits.esriMeters, r));
                    if (xyUnits.ToUpper() == "FEET") r = r / 0.3048; //convert to feet
                    break;
                case "MILES":
                    parameters.Add(new GPLinearUnit("Distance", esriUnits.esriMiles, r));
                    r = (xyUnits.ToUpper() == "FEET") ? r * 5280 : r * 1609.344; //
                    break;
                default:
                    MessageBox.Show(string.Format("Invalid Map Units: {0}. FEET, METERS or MILES are valid", cboUnitsR.SelectedItem.ToString().ToUpper()));

                    break;
            }
            double cellSize_ReSample = (r / cellSize > pixels / 2) ? r / (pixels / 2) : cellSize;
            parameters.Add(new GPString("CellSize", cellSize_ReSample.ToString()));

            vsGP viewshedGP = (vsGP)cmbGP.SelectedItem;
            _geoprocessorTask = new Geoprocessor(viewshedGP.Url );
            _geoprocessorTask.ExecuteCompleted += GeoprocessorTask_ExecuteCompleted;
            _geoprocessorTask.Failed += GeoprocessorTask_Failed;
            _geoprocessorTask.ExecuteAsync(parameters);
            mainPage.busyToggle(true);
        }
0 Kudos
1 Reply
quzijing
New Contributor
I just upgraded our custom Silverlight application from ArcGIS Silverlight API 2.4 to 3.1.  For some reason, one of our geoprocessing tasks is returning a ArgumentNullException, indicating that Parameter name: SpatialReference is null when the async task is executed.  I stepped through the code and saw that the spatialreference for one of the parameters was missing, so I assigned it in code but I'm still getting the error.  Any ideas?  My code is below:


private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (graphicsLayerPoly.Graphics.Count == 0)
            {
                MessageBox.Show("Please click on map to define Observer location.", "Viewshed", MessageBoxButton.OK);
                return;
            }
            LayoutRoot.Cursor = System.Windows.Input.Cursors.Wait;
            MapControl.Cursor = Cursors.Wait;

            MainPage mainPage = Application.Current.RootVisual as MainPage;
            string mapUnits = mainPage.scaleBar.MapUnit.ToString();

            List<GPParameter> parameters = new List<GPParameter>();

            double h = Convert.ToDouble(txtHeight.Text);
            if (cboUnitsH.SelectedItem.ToString().ToUpper() == "FEET" && zUnit.ToUpper() == "METERS") //input FEET to METERS
            {
                h = h * 0.3048;
            }
            else if ((cboUnitsH.SelectedItem.ToString().ToUpper() == "METERS" && zUnit.ToUpper() == "FEET")) //input METERS to FEET
            {
                h = h / 0.3048;
            }
            GPFeatureRecordSetLayer gpInputPoint = new GPFeatureRecordSetLayer("InputPoint", graphicsLayerPt.Graphics.First().Geometry);

            gpInputPoint.FeatureSet.Features.First().Attributes.Add("SPOT", (Convert.ToDouble(h) + Convert.ToDouble(txtHeight.Tag)).ToString());

            gpInputPoint.FeatureSet.Features.First().Attributes.Add("OFFSETA", 0);
            parameters.Add(gpInputPoint);

            GPFeatureRecordSetLayer gpInputArea = new GPFeatureRecordSetLayer("InputPolygon", graphicsLayerPoly.Graphics.First().Geometry);

            gpInputArea.FeatureSet.SpatialReference = gpInputPoint.FeatureSet.SpatialReference;  //here's where I assigned the spatialreference

            parameters.Add(gpInputArea);
            double r = Convert.ToDouble(txtRadius.Text);
            switch (cboUnitsR.SelectedItem.ToString().ToUpper())
            {
                case "FEET":
                    parameters.Add(new GPLinearUnit("Distance", esriUnits.esriFeet, r));
                    if (xyUnits.ToUpper() == "METERS") r = r * 0.3048; //convert to meters
                    break;
                case "METERS":
                    parameters.Add(new GPLinearUnit("Distance", esriUnits.esriMeters, r));
                    if (xyUnits.ToUpper() == "FEET") r = r / 0.3048; //convert to feet
                    break;
                case "MILES":
                    parameters.Add(new GPLinearUnit("Distance", esriUnits.esriMiles, r));
                    r = (xyUnits.ToUpper() == "FEET") ? r * 5280 : r * 1609.344; //
                    break;
                default:
                    MessageBox.Show(string.Format("Invalid Map Units: {0}. FEET, METERS or MILES are valid", cboUnitsR.SelectedItem.ToString().ToUpper()));

                    break;
            }
            double cellSize_ReSample = (r / cellSize > pixels / 2) ? r / (pixels / 2) : cellSize;
            parameters.Add(new GPString("CellSize", cellSize_ReSample.ToString()));

            vsGP viewshedGP = (vsGP)cmbGP.SelectedItem;
            _geoprocessorTask = new Geoprocessor(viewshedGP.Url );
            _geoprocessorTask.ExecuteCompleted += GeoprocessorTask_ExecuteCompleted;
            _geoprocessorTask.Failed += GeoprocessorTask_Failed;
            _geoprocessorTask.ExecuteAsync(parameters);
            mainPage.busyToggle(true);
        }


Making sure your  'gpInputPoint.FeatureSet.SpatialReference' was't null or you can try like this:gpInputArea.FeatureSet.SpatialReference=new ESRI.ArcGIS.Client.Geometry.SpatialReference(WKID='Your WKID');
Good Luck!
0 Kudos