Select to view content in your preferred language

I am missing something in my "Select by Extents" spatial query

1123
7
Jump to solution
03-03-2012 09:28 PM
SierkOudemans
Occasional Contributor
Hi,

This is based on the "Attribute Inspector (View)" Sample.  I have been at this for a while now, slowly constructing the application to selects a point feature rather than a polygon.  But I am missing something.  I got the "map_mapClickHandler" function to work. The "myFeatureLayer_selectionCompleteHandler" function returns the the selected feature's object ID but I get a "Error #1009: Cannot access a property or method of a null object reference." And I have no clue why.  Thanks for your help.


Here is the code:
<fx:Script>
  <![CDATA[
   import com.esri.ags.Graphic;
   import com.esri.ags.SpatialReference;
   import com.esri.ags.events.FeatureLayerEvent;
   import com.esri.ags.events.MapMouseEvent;
   import com.esri.ags.geometry.Extent;
   import com.esri.ags.geometry.Geometry;
   import com.esri.ags.geometry.MapPoint;
   import com.esri.ags.geometry.Polygon;
   import com.esri.ags.symbols.SimpleFillSymbol;
   import mx.controls.Alert;
   import mx.collections.ArrayCollection;
   import mx.rpc.events.FaultEvent;
  
   protected function map_mapClickHandler(event:MapMouseEvent):void
   {
    // convert click to an extents
    var vPt:MapPoint = event.mapPoint;
    var vX:Number = vPt.x;
    var vY:Number = vPt.y;
    var vExtent:Extent = new Extent(vX - 100, vY - 100,vX + 100, vY + 100)
    var thisSelectionQuery:Query = new Query();
    thisSelectionQuery.geometry = vExtent;
    myFeatureLayer.selectFeatures(thisSelectionQuery, FeatureLayer.SELECTION_NEW);
   }
  
   protected function attrInsp_faultHandler(event:FaultEvent):void
   {
    Alert.show(event.fault.message, "Fault");
   }
  
   protected function myFeatureLayer_selectionCompleteHandler(event:FeatureLayerEvent):void
   {
    // only show infoWindow if a feature was found
    if (event.featureLayer.numGraphics > 0)
    {
     myMap.infoWindow.label = "Sign " + event.featureLayer.selectedFeatures[0].attributes.OBJECTID;
     //I am not sure what I should do with this
     myMap.infoWindow.show(queryMapClick.geometry as MapPoint);
    }
    else
    {
     Alert.show("Sorry found nothing here...");
    }
   }
  ]]>
</fx:Script>
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus
Sierk,

   So here is the cahnges you need to make:

<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"                xmlns:s="library://ns.adobe.com/flex/spark"                xmlns:esri="http://www.esri.com/2008/ags"                pageTitle="Attribute Inspector (edit)">     <fx:Script>         <![CDATA[                          import com.esri.ags.Graphic;             import com.esri.ags.SpatialReference;             import com.esri.ags.events.FeatureLayerEvent;             import com.esri.ags.events.MapMouseEvent;             import com.esri.ags.geometry.Extent;             import com.esri.ags.geometry.Geometry;             import com.esri.ags.geometry.MapPoint;             import com.esri.ags.geometry.Polygon;             import com.esri.ags.symbols.SimpleFillSymbol;                          import mx.collections.ArrayCollection;             import mx.controls.Alert;             import mx.rpc.events.FaultEvent;                          private var mapClickPoint:MapPoint;                          protected function map_mapClickHandler(event:MapMouseEvent):void             {                 // convert click to an extents                 var vPt:MapPoint = event.mapPoint;                 mapClickPoint = event.mapPoint;                 var vX:Number = vPt.x;                 var vY:Number = vPt.y;                 var vExtent:Extent = new Extent(vX - 100, vY - 100,vX + 100, vY + 100)                 var thisSelectionQuery:Query = new Query();                 thisSelectionQuery.geometry = vExtent;                 myFeatureLayer.selectFeatures(thisSelectionQuery, FeatureLayer.SELECTION_NEW);             }                          protected function attrInsp_faultHandler(event:FaultEvent):void             {                 Alert.show(event.fault.message, "Fault");             }                          protected function myFeatureLayer_selectionCompleteHandler(event:FeatureLayerEvent):void             {                 // only show infoWindow if a feature was found                 if (event.featureLayer.numGraphics > 0){                     myMap.infoWindow.label = "Sign " + event.featureLayer.selectedFeatures[0].attributes.OBJECTID;                     myMap.infoWindow.show(mapClickPoint);                 }else{                     Alert.show("Sorry found nothing here...");                 }             }         ]]>     </fx:Script>          <fx:Declarations>         <esri:Query id="queryMapClick"/>     </fx:Declarations>          <fx:Style>         @namespace s "library://ns.adobe.com/flex/spark";         @namespace esri "http://www.esri.com/2008/ags";                  esri|InfoWindow         {             background-color: #BDB76B; /* dark khaki */             border-color: #000000;             border-thickness: 3;             lower-left-radius: 20;             lower-right-radius: 20;             upper-left-radius: 20;             upper-right-radius: 0;             padding-bottom: 7;             padding-left: 7;             padding-right: 7;             padding-top: 4;         }         esri|InfoWindowLabel         {             color: #FFFFFF;             font-size: 15;             padding-left: 120;         }         s|BorderContainer         {             background-color: #F0E68C; /* light khaki */             corner-radius: 15;             content-background-color: #BDB76B;             padding-bottom: 5;         }     </fx:Style>          <s:layout>         <s:VerticalLayout/>     </s:layout>     <esri:Map id="myMap"               load="attrInsp.featureLayers = [myFeatureLayer]"               mapClick="map_mapClickHandler(event)"               openHandCursorVisible="false">         <!-- Extent for Woukesha Signs -->         <esri:extent>             <esri:Extent xmin="2468000" ymin="371000" xmax="2475000" ymax="376000">                 <esri:SpatialReference wkid="32054"/>             </esri:Extent>         </esri:extent>         <esri:ArcGISDynamicMapServiceLayer             url="https://maps.ags.ruekert-mielke.com/AGS/rest/services/WaukeshaCity/WaukeshaSignsSampleSigns/MapServer"/>                  <esri:ArcGISDynamicMapServiceLayer             url="https://maps.ags.ruekert-mielke.com/AGS/rest/services/WaukeshaCity/WaukeshaSignsSampleApp2/MapServer"/>                  <esri:FeatureLayer id="myFeatureLayer"                            mode="selection"                            outFields="[HouseNumber,StreetName]"                            selectionColor="0x000000"                            selectionComplete="myFeatureLayer_selectionCompleteHandler(event)"                            url="https://maps.ags.ruekert-mielke.com/AGS/rest/services/WaukeshaCity/WaukeshaSignsSampleSigns/MapServer/0"/>                  <esri:GraphicsLayer id="myGraphicsLayer">             <esri:Graphic>                 <esri:geometry>                     <esri:MapPoint x="2471315" y="374682" spatialReference="{new SpatialReference(32054)}"/>                 </esri:geometry>             </esri:Graphic>         </esri:GraphicsLayer>                  <esri:infoWindowContent>             <s:BorderContainer>                 <esri:AttributeInspector id="attrInsp"/>             </s:BorderContainer>         </esri:infoWindowContent>              </esri:Map>     <s:Label text="The feature layer has {myFeatureLayer.selectedFeatures.length} selected feature(s)."/>      </s:Application>


Don't forget to click the Mark as answer check and to click the top arrow (promote) as shown below:

View solution in original post

0 Kudos
7 Replies
IvanBespalov
Frequent Contributor
Sierk,

Cannot access a property or method of a null object reference.


just fill the difference:

output: "Hello world!"
try
{
    var myObject:Object = new Object(); // myObject is not null
    myObject.myParameter = "Hello world!";

    var message:String = myObject.myParameter;
    trace(message);
}
catch (error:Error)
{
    trace(error.message.toString());
}

output: "Error #1009: Cannot access a property or method of a null object reference."
try
{
    var myObject:Object = new Object(); // myObject is null

    var message:String = myObject.myParameter;
    trace(message);
}
catch (error:Error)
{
    trace(error.message.toString());
}


How to debug your application - adobe help
Add and remove breakpoints - adobe help
The Flash Debug perspective (Variables view) - adobe help
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Sierk,

   Assumin that the queryMapClick is a private var somewhere in the code that you did not attach than all you have to do is the add the line in red below. If not then you need to add a private var for queryMapClick.


    <fx:Script>
        <![CDATA[
            import com.esri.ags.Graphic;
            import com.esri.ags.SpatialReference;
            import com.esri.ags.events.FeatureLayerEvent;
            import com.esri.ags.events.MapMouseEvent;
            import com.esri.ags.geometry.Extent;
            import com.esri.ags.geometry.Geometry;
            import com.esri.ags.geometry.MapPoint;
            import com.esri.ags.geometry.Polygon;
            import com.esri.ags.layers.FeatureLayer;
            import com.esri.ags.symbols.SimpleFillSymbol;
            
            import mx.collections.ArrayCollection;
            import mx.controls.Alert;
            import mx.rpc.events.FaultEvent;
            
            protected function map_mapClickHandler(event:MapMouseEvent):void
            {
                // convert click to an extents
                var vPt:MapPoint = event.mapPoint;
                queryMapClick = event.mapPoint;
                var vX:Number = vPt.x;
                var vY:Number = vPt.y;
                var vExtent:Extent = new Extent(vX - 100, vY - 100,vX + 100, vY + 100)
                var thisSelectionQuery:Query = new Query();
                thisSelectionQuery.geometry = vExtent;
                var myFeatureLayer:FeatureLayer = new FeatureLayer;
            }
            
            protected function attrInsp_faultHandler(event:FaultEvent):void
            {
                Alert.show(event.fault.message, "Fault");
            }
            
            protected function myFeatureLayer_selectionCompleteHandler(event:FeatureLayerEvent):void
            {
                // only show infoWindow if a feature was found
                if (event.featureLayer.numGraphics > 0)
                {
                    myMap.infoWindow.label = "Sign " + event.featureLayer.selectedFeatures[0].attributes.OBJECTID;
                    //I am not sure what I should do with this
                    myMap.infoWindow.show(queryMapClick.geometry as MapPoint);
                }
                else
                {
                    Alert.show("Sorry found nothing here...");
                }
            }
        ]]>
    </fx:Script>


Don't forget to click the Mark as answer check and to click the top arrow (promote) as shown below:
0 Kudos
SierkOudemans
Occasional Contributor
Thanks rscheitlin for your reply.

The variable is declared in the declaration section:
<fx:Declarations>
 <esri:Query id="queryMapClick"/>

</fx:Declarations>


and I get an error when I implement your suggestion:

1067: Implicit coercion of a value of type com.esri.ags.geometry:MapPoint to an unrelated type com.esri.ags.tasks.supportClasses:Query


I tried finding an answer to this question but found none that explained it in such a way I could understand what is going on.  I am kind of new at this.  How would I resolve this?

Thanks
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Sierk,

   Well I did not expect the queryMapClick to be a Query at all... You should copy ALL the code and paste it into a text document and then attach it to this thread.
0 Kudos
SierkOudemans
Occasional Contributor
Thanks Robert  for watching this thread.  here is all of the code:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
      xmlns:s="library://ns.adobe.com/flex/spark"
      xmlns:esri="http://www.esri.com/2008/ags"
      pageTitle="Attribute Inspector (edit)">
 <fx:Script>
  <![CDATA[

   import com.esri.ags.Graphic;
   import com.esri.ags.SpatialReference;
   import com.esri.ags.events.FeatureLayerEvent;
   import com.esri.ags.events.MapMouseEvent;
   import com.esri.ags.geometry.Extent;
   import com.esri.ags.geometry.Geometry;
   import com.esri.ags.geometry.MapPoint;
   import com.esri.ags.geometry.Polygon;
   import com.esri.ags.symbols.SimpleFillSymbol;
   
   import mx.collections.ArrayCollection;
   import mx.controls.Alert;
   import mx.rpc.events.FaultEvent;
   //public var queryMapClick1:MapPoint;
   
   
   protected function map_mapClickHandler(event:MapMouseEvent):void
   {
    // convert click to an extents
    var vPt:MapPoint = event.mapPoint;
    queryMapClick = event.mapPoint;
    var vX:Number = vPt.x;
    var vY:Number = vPt.y;
    var vExtent:Extent = new Extent(vX - 100, vY - 100,vX + 100, vY + 100)
    var thisSelectionQuery:Query = new Query();
    thisSelectionQuery.geometry = vExtent;
    myFeatureLayer.selectFeatures(thisSelectionQuery, FeatureLayer.SELECTION_NEW);
   }
   
   protected function attrInsp_faultHandler(event:FaultEvent):void
   {
    Alert.show(event.fault.message, "Fault");
   }
   
   protected function myFeatureLayer_selectionCompleteHandler(event:FeatureLayerEvent):void
   {
    // only show infoWindow if a feature was found
    if (event.featureLayer.numGraphics > 0)
    {
     myMap.infoWindow.label = "Sign " + event.featureLayer.selectedFeatures[0].attributes.OBJECTID;
     //I am not sure what I should do with this
     myMap.infoWindow.show(queryMapClick.geometry as MapPoint);
    }
    else
    {
     Alert.show("Sorry found nothing here...");
    }
   }
  ]]>
 </fx:Script>
 
 <fx:Declarations>
  <esri:Query id="queryMapClick"/>
 </fx:Declarations>
 
 <fx:Style>
  @namespace s "library://ns.adobe.com/flex/spark";
  @namespace esri "http://www.esri.com/2008/ags";
  
  esri|InfoWindow
  {
   background-color: #BDB76B; /* dark khaki */
   border-color: #000000;
   border-thickness: 3;
   lower-left-radius: 20;
   lower-right-radius: 20;
   upper-left-radius: 20;
   upper-right-radius: 0;
   padding-bottom: 7;
   padding-left: 7;
   padding-right: 7;
   padding-top: 4;
  }
  esri|InfoWindowLabel
  {
   color: #FFFFFF;
   font-size: 15;
   padding-left: 120;
  }
  s|BorderContainer
  {
   background-color: #F0E68C; /* light khaki */
   corner-radius: 15;
   content-background-color: #BDB76B;
   padding-bottom: 5;
  }
 </fx:Style>
 
 <s:layout>
  <s:VerticalLayout/>
 </s:layout>
 <esri:Map id="myMap"
     load="attrInsp.featureLayers = [myFeatureLayer]"
     mapClick="map_mapClickHandler(event)"
     openHandCursorVisible="false">
  <!-- Extent for Woukesha Signs -->
  <esri:extent>
   <esri:Extent xmin="2468000" ymin="371000" xmax="2475000" ymax="376000">
    <esri:SpatialReference wkid="32054"/>
   </esri:Extent>
  </esri:extent>
  <esri:ArcGISDynamicMapServiceLayer
   url="https://maps.ags.ruekert-mielke.com/AGS/rest/services/WaukeshaCity/WaukeshaSignsSampleSigns/MapServer"/>
  
  <esri:ArcGISDynamicMapServiceLayer
   url="https://maps.ags.ruekert-mielke.com/AGS/rest/services/WaukeshaCity/WaukeshaSignsSampleApp2/MapServer"/>
 
  <esri:FeatureLayer id="myFeatureLayer"
        mode="selection"
        outFields="[HouseNumber,StreetName]"
        selectionColor="0x000000"
        selectionComplete="myFeatureLayer_selectionCompleteHandler(event)"
        url="https://maps.ags.ruekert-mielke.com/AGS/rest/services/WaukeshaCity/WaukeshaSignsSampleSigns/MapServer/0"/>

  <esri:GraphicsLayer id="myGraphicsLayer">
   <esri:Graphic>
    <esri:geometry>
     <esri:MapPoint x="2471315" y="374682" spatialReference="{new SpatialReference(32054)}"/>
    </esri:geometry>
   </esri:Graphic>
  </esri:GraphicsLayer>
  
  <esri:infoWindowContent>
   <s:BorderContainer>
    <esri:AttributeInspector id="attrInsp"/>
   </s:BorderContainer>
  </esri:infoWindowContent>
  
 </esri:Map>
 <s:Label text="The feature layer has {myFeatureLayer.selectedFeatures.length} selected feature(s)."/>
 
</s:Application>
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Sierk,

   So here is the cahnges you need to make:

<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"                xmlns:s="library://ns.adobe.com/flex/spark"                xmlns:esri="http://www.esri.com/2008/ags"                pageTitle="Attribute Inspector (edit)">     <fx:Script>         <![CDATA[                          import com.esri.ags.Graphic;             import com.esri.ags.SpatialReference;             import com.esri.ags.events.FeatureLayerEvent;             import com.esri.ags.events.MapMouseEvent;             import com.esri.ags.geometry.Extent;             import com.esri.ags.geometry.Geometry;             import com.esri.ags.geometry.MapPoint;             import com.esri.ags.geometry.Polygon;             import com.esri.ags.symbols.SimpleFillSymbol;                          import mx.collections.ArrayCollection;             import mx.controls.Alert;             import mx.rpc.events.FaultEvent;                          private var mapClickPoint:MapPoint;                          protected function map_mapClickHandler(event:MapMouseEvent):void             {                 // convert click to an extents                 var vPt:MapPoint = event.mapPoint;                 mapClickPoint = event.mapPoint;                 var vX:Number = vPt.x;                 var vY:Number = vPt.y;                 var vExtent:Extent = new Extent(vX - 100, vY - 100,vX + 100, vY + 100)                 var thisSelectionQuery:Query = new Query();                 thisSelectionQuery.geometry = vExtent;                 myFeatureLayer.selectFeatures(thisSelectionQuery, FeatureLayer.SELECTION_NEW);             }                          protected function attrInsp_faultHandler(event:FaultEvent):void             {                 Alert.show(event.fault.message, "Fault");             }                          protected function myFeatureLayer_selectionCompleteHandler(event:FeatureLayerEvent):void             {                 // only show infoWindow if a feature was found                 if (event.featureLayer.numGraphics > 0){                     myMap.infoWindow.label = "Sign " + event.featureLayer.selectedFeatures[0].attributes.OBJECTID;                     myMap.infoWindow.show(mapClickPoint);                 }else{                     Alert.show("Sorry found nothing here...");                 }             }         ]]>     </fx:Script>          <fx:Declarations>         <esri:Query id="queryMapClick"/>     </fx:Declarations>          <fx:Style>         @namespace s "library://ns.adobe.com/flex/spark";         @namespace esri "http://www.esri.com/2008/ags";                  esri|InfoWindow         {             background-color: #BDB76B; /* dark khaki */             border-color: #000000;             border-thickness: 3;             lower-left-radius: 20;             lower-right-radius: 20;             upper-left-radius: 20;             upper-right-radius: 0;             padding-bottom: 7;             padding-left: 7;             padding-right: 7;             padding-top: 4;         }         esri|InfoWindowLabel         {             color: #FFFFFF;             font-size: 15;             padding-left: 120;         }         s|BorderContainer         {             background-color: #F0E68C; /* light khaki */             corner-radius: 15;             content-background-color: #BDB76B;             padding-bottom: 5;         }     </fx:Style>          <s:layout>         <s:VerticalLayout/>     </s:layout>     <esri:Map id="myMap"               load="attrInsp.featureLayers = [myFeatureLayer]"               mapClick="map_mapClickHandler(event)"               openHandCursorVisible="false">         <!-- Extent for Woukesha Signs -->         <esri:extent>             <esri:Extent xmin="2468000" ymin="371000" xmax="2475000" ymax="376000">                 <esri:SpatialReference wkid="32054"/>             </esri:Extent>         </esri:extent>         <esri:ArcGISDynamicMapServiceLayer             url="https://maps.ags.ruekert-mielke.com/AGS/rest/services/WaukeshaCity/WaukeshaSignsSampleSigns/MapServer"/>                  <esri:ArcGISDynamicMapServiceLayer             url="https://maps.ags.ruekert-mielke.com/AGS/rest/services/WaukeshaCity/WaukeshaSignsSampleApp2/MapServer"/>                  <esri:FeatureLayer id="myFeatureLayer"                            mode="selection"                            outFields="[HouseNumber,StreetName]"                            selectionColor="0x000000"                            selectionComplete="myFeatureLayer_selectionCompleteHandler(event)"                            url="https://maps.ags.ruekert-mielke.com/AGS/rest/services/WaukeshaCity/WaukeshaSignsSampleSigns/MapServer/0"/>                  <esri:GraphicsLayer id="myGraphicsLayer">             <esri:Graphic>                 <esri:geometry>                     <esri:MapPoint x="2471315" y="374682" spatialReference="{new SpatialReference(32054)}"/>                 </esri:geometry>             </esri:Graphic>         </esri:GraphicsLayer>                  <esri:infoWindowContent>             <s:BorderContainer>                 <esri:AttributeInspector id="attrInsp"/>             </s:BorderContainer>         </esri:infoWindowContent>              </esri:Map>     <s:Label text="The feature layer has {myFeatureLayer.selectedFeatures.length} selected feature(s)."/>      </s:Application>


Don't forget to click the Mark as answer check and to click the top arrow (promote) as shown below:
0 Kudos
SierkOudemans
Occasional Contributor
Thanks Robert,

That did the trick.  Have a great day.

Sierk
0 Kudos