ThinkGeo.com    |     Documentation    |     Premium Support

Null Exception when using labels

I have been experiencing many "NullRefrence Exceptions not handled"


I have narrowed it down to using labels, It does not seem to matter if I use the built in TextStyles or a custom TextStyle. The errors occur during redraws (Panning, Zoom In, and Zoom Out) . There does not seem to be a common way to duplicate and seem to occur randomly - In other words, If I perform a given zoom and once and receive an error, It may or may not occur again the next time even though I repeat the steps exactly as the first time.


Below is the ouput from the exception.


System.NullReferenceException was unhandled

  Message="Object reference not set to an instance of an object."

  Source="MapSuiteCore"

  StackTrace:

       at ThinkGeo.MapSuite.Core.PositionStyle.x5e8ba87a6e4f7872(PolygonShape xdd1257604b85b26a, String x8d11a5f0a2282818, GeoCanvas x31c084515ae9393f)

       at ThinkGeo.MapSuite.Core.PositionStyle.xf2594a7578927384(MultipolygonShape x3ebe6da76843c14a, String x8d11a5f0a2282818, GeoCanvas x31c084515ae9393f)

       at ThinkGeo.MapSuite.Core.PositionStyle.xa560424e1568ae1a(MultipolygonShape x3ebe6da76843c14a, String x8d11a5f0a2282818, GeoCanvas x31c084515ae9393f)

       at ThinkGeo.MapSuite.Core.PositionStyle.GetLabelingCandidateCore(Feature feature, GeoCanvas canvas)

       at ThinkGeo.MapSuite.Core.PositionStyle.GetLabelingCandidates(Feature feature, GeoCanvas canvas)

       at ThinkGeo.MapSuite.Core.PositionStyle.xf735974b04b9e074(Feature x79fd310d5eca45f6, GeoCanvas x31c084515ae9393f, Collection`1 x00f17d7e921affcc, Collection`1 xd03d0815a07e11bc)

       at ThinkGeo.MapSuite.Core.PositionStyle.DrawCore(IEnumerable`1 features, GeoCanvas canvas, Collection`1 labelsInThisLayer, Collection`1 labelsInAllLayers)

       at ThinkGeo.MapSuite.Core.Style.Draw(IEnumerable`1 features, GeoCanvas canvas, Collection`1 labelsInThisLayer, Collection`1 labelsInAllLayers)

       at ThinkGeo.MapSuite.Core.ZoomLevel.DrawCore(GeoCanvas canvas, IEnumerable`1 features, Collection`1 currentLayerLabels, Collection`1 allLayerLabels)

       at ThinkGeo.MapSuite.Core.ZoomLevel.Draw(GeoCanvas canvas, IEnumerable`1 features, Collection`1 currentLayerLabels, Collection`1 allLayerLabels)

       at ThinkGeo.MapSuite.Core.FeatureLayer.DrawCore(GeoCanvas canvas, Collection`1 labelsInAllLayers)

       at ThinkGeo.MapSuite.Core.Layer.Draw(GeoCanvas canvas, Collection`1 labelsInAllLayers)

       at ThinkGeo.MapSuite.DesktopEdition.LayerOverlay.xa6cb874f7090c07f(GeoCanvas x31c084515ae9393f)

       at ThinkGeo.MapSuite.DesktopEdition.LayerOverlay.DrawCore(GeoCanvas canvas)

       at ThinkGeo.MapSuite.DesktopEdition.Overlay.Draw(GeoCanvas canvas)

       at ThinkGeo.MapSuite.DesktopEdition.xf0380b1a0bc40ca6.StartDraw()

       at ThinkGeo.MapSuite.DesktopEdition.x601a5561df898600.StartDraw(Int32 tileWidth, Int32 tileHeight, RectangleShape tileExtent, Collection`1 overlays)

       at ThinkGeo.MapSuite.DesktopEdition.x601a5561df898600.DrawAddedExtents(IEnumerable`1 addedExtents, Collection`1 overlays)

       at ThinkGeo.MapSuite.DesktopEdition.x65fdca92d79c01f5.Refresh(x177b8d80e88ce3a2 tempRedrawMode)

       at ThinkGeo.MapSuite.DesktopEdition.x65fdca92d79c01f5.DrawNewImage(Int32 delayInterval)

       at ThinkGeo.MapSuite.DesktopEdition.x65fdca92d79c01f5.delayDrawingTimer_Tick(Object sender, EventArgs e)

       at System.Windows.Forms.Timer.OnTick(EventArgs e)

       at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)

       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)

       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)

       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)

       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)

       at System.Windows.Forms.Application.Run(ApplicationContext context)

       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()

       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()

       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)

       at KTrac3.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81

       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)

       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)

       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()

       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)

       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

       at System.Threading.ThreadHelper.ThreadStart()

  InnerException: 

 



Am I the only one with this problem???

Alan, 
  
 We can’t recreate this issue. Could you please tell more about how you setup the style? It’s better if you can provide a demo even if it goes into the issue randomly. 
  
 Thanks,  
 ThinkGeo Support 


I have narrowed it down to the AllowLineCarriage property in the TextStyle class 
  
     Private Function GetHotelTextStyle(ByVal FieldName As String, Optional ByVal FontSize As Integer = 10, Optional ByVal FontName As String = “Arial”) As TextStyle 
  
         Dim ts As TextStyle = TextStyles.CreateSimpleTextStyle(FieldName, FontName, FontSize, FontStyle.Bold, GeoColor.SimpleColors.Blue, GeoColor.StandardColors.White, 2) 
         ts.OverlappingRule = LabelOverlappingRule.NoOverlapping 
         ts.AllowLineCarriage = True 'this will ramdomly cause NullReferenceException 
         ts.ForceLineCarriage = True 'works fine with this. 
         ts.HaloPen.LineJoin = DrawingLineJoin.Round 
         Return ts 
  
     End Function

Thanks, we will look into this. 
  
 ThinkGeo Support 


I am also getting this exception occasionally on zooms and also on resize events.



Alan & David, 
  
 I still can not recreate this issue using your code. And I noticed that you are not using the current release version (3.0.199) as the 4th parameter of method CreateSimpleTextStyle () is DrawingFontStyles in 3.0.199. Could you please check your version?  
  
 You can download the latest version from gis.thinkgeo.com/Products/MapSuiteFreeTrialDownloads/tabid/152/Default.aspx  
  
 Thanks, 
 ThinkGeo Support 


I am using 3.0.199.  I set 'AllowLineCarriage' back to true and the exception occurs when zooming out (sometimes once, sometimes several times...threading???)


Exception:


 



System.NullReferenceException was unhandled
  Message="Object reference not set to an instance of an object."
  Source="MapSuiteCore"
  StackTrace:
       at ThinkGeo.MapSuite.Core.PositionStyle.x5e8ba87a6e4f7872(PolygonShape xdd1257604b85b26a, String x8d11a5f0a2282818, GeoCanvas x31c084515ae9393f)
       at ThinkGeo.MapSuite.Core.PositionStyle.xa560424e1568ae1a(MultipolygonShape x3ebe6da76843c14a, String x8d11a5f0a2282818, GeoCanvas x31c084515ae9393f)
       at ThinkGeo.MapSuite.Core.PositionStyle.GetLabelingCandidateCore(Feature feature, GeoCanvas canvas)
       at ThinkGeo.MapSuite.Core.PositionStyle.xf735974b04b9e074(Feature x79fd310d5eca45f6, GeoCanvas x31c084515ae9393f, Collection`1 x00f17d7e921affcc, Collection`1 xd03d0815a07e11bc)
       at ThinkGeo.MapSuite.Core.PositionStyle.DrawCore(IEnumerable`1 features, GeoCanvas canvas, Collection`1 labelsInThisLayer, Collection`1 labelsInAllLayers)
       at ThinkGeo.MapSuite.Core.Style.Draw(IEnumerable`1 features, GeoCanvas canvas, Collection`1 labelsInThisLayer, Collection`1 labelsInAllLayers)
       at ThinkGeo.MapSuite.Core.ZoomLevel.DrawCore(GeoCanvas canvas, IEnumerable`1 features, Collection`1 currentLayerLabels, Collection`1 allLayerLabels)
       at ThinkGeo.MapSuite.Core.ZoomLevel.Draw(GeoCanvas canvas, IEnumerable`1 features, Collection`1 currentLayerLabels, Collection`1 allLayerLabels)
       at ThinkGeo.MapSuite.Core.FeatureLayer.DrawCore(GeoCanvas canvas, Collection`1 labelsInAllLayers)
       at ThinkGeo.MapSuite.Core.Layer.Draw(GeoCanvas canvas, Collection`1 labelsInAllLayers)
       at ThinkGeo.MapSuite.DesktopEdition.LayerOverlay.xa6cb874f7090c07f(GeoCanvas x31c084515ae9393f)
       at ThinkGeo.MapSuite.DesktopEdition.LayerOverlay.DrawCore(GeoCanvas canvas)
       at ThinkGeo.MapSuite.DesktopEdition.Overlay.Draw(GeoCanvas canvas)
       at ThinkGeo.MapSuite.DesktopEdition.xf0380b1a0bc40ca6.StartDraw()
       at ThinkGeo.MapSuite.DesktopEdition.x601a5561df898600.StartDraw(Int32 tileWidth, Int32 tileHeight, RectangleShape tileExtent, Collection`1 overlays)
       at ThinkGeo.MapSuite.DesktopEdition.x601a5561df898600.DrawAddedExtents(IEnumerable`1 addedExtents, Collection`1 overlays)
       at ThinkGeo.MapSuite.DesktopEdition.x65fdca92d79c01f5.Refresh(x177b8d80e88ce3a2 tempRedrawMode)
       at ThinkGeo.MapSuite.DesktopEdition.x65fdca92d79c01f5.DrawNewImage(Int32 delayInterval)
       at ThinkGeo.MapSuite.DesktopEdition.x65fdca92d79c01f5.delayDrawingTimer_Tick(Object sender, EventArgs e)
       at System.Windows.Forms.Timer.OnTick(EventArgs e)
       at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at ActiveMap.Program.Main() in C:\CMS\ActiveMap\Program.cs:line 17
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

 


TextStyle setup:



                  if ( zl.Text != null )
                  {
                     // Text for zoom level is defined
                     GeoColor gcolor = GeoColor.FromArgb(zl.Text.Color.A, zl.Text.Color.R,
                                          zl.Text.Color.G, zl.Text.Color.B);
                     long inverseA, inverseR, inverseG, inverseB;
                     Math.DivRem(gcolor.AlphaComponent, 127, out inverseA);
                     Math.DivRem(gcolor.BlueComponent, 127, out inverseB);
                     Math.DivRem(gcolor.GreenComponent, 127, out inverseG);
                     Math.DivRem(gcolor.RedComponent, 127, out inverseR);
                     GeoColor outColor = GeoColor.FromArgb((int)inverseA, (int)inverseR,
                                                            (int)inverseG, (int)inverseB);
                     string textColumn = zl.Text.TextFields; //.Trim(new char [] { '[', ']' });
                     TextStyle textStyle = TextStyles.CreateSimpleTextStyle(textColumn,
                                             zl.Text.Font.FontFamily.Name, zl.Text.Font.Size,
                                             FontStyleToDrawingStyle(zl.Text.Font.Style), gcolor, outColor, 2, 0, -2);
                     textStyle.OverlappingRule = LabelOverlappingRule.NoOverlapping;
                     textStyle.PointPlacement = PointPlacement.UpperRight;
                     textStyle.SuppressPartialLabels = true;
                     textStyle.DuplicateRule = LabelDuplicateRule.UnlimitedDuplicateLabels;
                     textStyle.AllowLineCarriage = true; // according to blog true causes null exceptions on random zooms in Beta
                     textStyle.AllowSpline = true;
                     textStyle.BestPlacement = true;
                     pzl.CustomStyles.Add(textStyle);
                  }

 


The enumerations of DrawingFontStyles does not equate to System.Drawing.FontStyle so I added a helper function (below).  Same with LineDashStyle to System.Drawing.Drawing2D.DashStyle.



      private DrawingFontStyles FontStyleToDrawingStyle( FontStyle style )
      {
         if ( style == FontStyle.Regular )
            return DrawingFontStyles.Regular;
         else if ( style == FontStyle.Bold )
            return DrawingFontStyles.Bold;
         else if ( style == FontStyle.Italic )
            return DrawingFontStyles.Italic;
         else if ( style == FontStyle.Strikeout )
            return DrawingFontStyles.Strikeout;
         else if ( style == FontStyle.Underline )
            return DrawingFontStyles.Underline;
         return DrawingFontStyles.Regular;
      }


 



Lee,


Yes, I am usiing 3.0.199. Actually I didn't begin testing until you included source for VB in the latest release. I can use either C# or VB, But I prefer VB.


I have been using the ForceLineCarriage instead of AllowLineCarriage because ForceLineCarriage doesn't throw this exception. I have been using this for about a week without errors.


In response to your question I set it back to using AllowLineCarriage and immediately got the same error as before. I also changed the Font Parameter from FontStyle.Bold to DrawingFontStyles.Bold and the error still occurs.


Note that it will accept either FontStyle.Bold or DrawingFontStyles.Bold and still works the same in either scenario.


The only common factor in creating and preventing the error is the AllowLineCarriage. As long as this set to false I can change any of the other properties and not get errors.


Alan



I have attached the shape file used when this error occurs. Hotel.shp


 



        Dim HotelLabelLayer As New ShapeFileFeatureLayer(DataPath & "Hotel.shp")
        With HotelLabelLayer.ZoomLevelSet
            .ZoomLevel15.CustomStyles.Add(GetHotelTextStyle("FAC_NAME"))
            .ZoomLevel15.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20
            Map1.StaticOverlay.Layers.Add("HotelLabel", HotelLabelLayer)
        End With

 



   Private Function GetHotelTextStyle(ByVal FieldName As String, Optional ByVal FontSize As Integer = 10, Optional ByVal FontName As String = "Arial") As TextStyle

        Dim HotelTextStyle As TextStyle = TextStyles.CreateSimpleTextStyle(FieldName, FontName, FontSize, DrawingFontStyles.Bold, GeoColor.SimpleColors.Blue, GeoColor.StandardColors.White, 2)
        HotelTextStyle.OverlappingRule = LabelOverlappingRule.NoOverlapping
        'HotelTextStyle.ForceLineCarriage = True 'works without errors
        HotelTextStyle.AllowLineCarriage = True 'randomly throws exception during changes in zoom
        HotelTextStyle.HaloPen.LineJoin = DrawingLineJoin.Round
        Return HotelTextStyle

    End Function


659-Hotel.zip (180 KB)

Thanks Alan, 
  
 This is a bug and we will fix it in next version. Sorry for the inconvenience for now. 
  
 ThinkGeo Support