ThinkGeo.com    |     Documentation    |     Premium Support

NullReferenceException was unhandled

Hi,


I am getting following exception when trying to add GraticuleAdornmentLayer with WrapDateline mode.

Am I doing something wrong?



Thanks,<br/ >
Vicky





      private void wpfMap1_Loaded(object sender, RoutedEventArgs e)
        {


            wpfMap1.MapUnit = GeographyUnit.Meter;


            string sMapShapeFileName = AppDomain.CurrentDomain.BaseDirectory + @"Resources\map\world.shp";
            string sMapShapeIndexFileName = AppDomain.CurrentDomain.BaseDirectory + @"Resources\map\world.idx";


           
            ShapeFileFeatureLayer worldLayer = new ShapeFileFeatureLayer(sMapShapeFileName, sMapShapeIndexFileName);
            worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Country1;
            worldLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle = LineStyles.InternationalBorder1;
            worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
         
            Proj4Projection proj4 = new Proj4Projection();
            proj4.InternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(4326);
            proj4.ExternalProjectionParametersString = Proj4Projection.GetGoogleMapParametersString();
            worldLayer.FeatureSource.Projection = proj4;
           
            worldLayer.Open();
            wpfMap1.CurrentExtent = worldLayer.GetBoundingBox();
            worldLayer.Close();

            LayerOverlay staticOverlay = new LayerOverlay();
            staticOverlay.TileType = TileType.SingleTile;
            staticOverlay.Layers.Add(new BackgroundLayer(new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean)));
            staticOverlay.Layers.Add("WorldLayer", worldLayer);
            staticOverlay.WrappingMode = WrappingMode.WrapDateline;
            wpfMap1.Overlays.Add(staticOverlay);


            GraticuleAdornmentLayer graticuleAdornmentLayer = new GraticuleAdornmentLayer();
            graticuleAdornmentLayer.GraticuleLineStyle = LineStyles.DegreeLine1;
            graticuleAdornmentLayer.WrappingMode = WrappingMode.WrapDateline;
            graticuleAdornmentLayer.Projection = proj4;
          
            wpfMap1.AdornmentOverlay.Layers.Add("graticule", graticuleAdornmentLayer);

        

          

            wpfMap1.Refresh();
          
       

        }
 
 

Exception:

System.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.
  Source=WpfDesktopEdition
  StackTrace:
       at ThinkGeo.MapSuite.WpfDesktopEdition.Tile.<>c__DisplayClass2.<DrawException>b__0()
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.DispatcherOperation.InvokeImpl()
       at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
       at System.Threading.ExecutionContext.runTryCode(Object userData)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.ProcessQueue()
       at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       at System.Windows.Application.RunDispatcher(Object ignore)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run(Window window)
       at System.Windows.Application.Run()
       at TestShapeFile.App.Main() in C:\_Development\VS2010\Apps\TestMapSuiteWPF\TestShapeFile\obj\x86\Debug\App.g.cs:line 0
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly 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, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

 


Vicky, 
  
   The issue is that as far as I know the GraticuleAdornmentLayer was not designed to work with the WrapDateLine mode.  When we wrap the dateline we change the internal coordinates of the points and the graticule logic was written before that and doesn’t know how to handle it.  We would work on this as an enhancement.  I can add it to the public enhancement tracker and see how many people vote to want that functionality or you have the option to work with professional services to pay to add the enhancement.  This is of course if you need it right away.  Most ideas end up making it into the product for free but we have to prioritize them based on what our user ask for, i.e. the enhancement trackers.   
  
 David

David,


I don’t think it is a WrapDateLine mode that causes an error, because it is working just fine when map unit in DecimalDegree (without projection). 

I disabled a WrapDateLine mode, and still having the same error on certain scales.


Please see following code that only  works from ZoomLevel04 and up.


Vicky




        private void wpfMap1_Loaded(object sender, RoutedEventArgs e)
        {
            wpfMap1.MapUnit = GeographyUnit.Meter;

            string sMapShapeFileName = AppDomain.CurrentDomain.BaseDirectory + @"Resources\map\world.shp";
            string sMapShapeIndexFileName = AppDomain.CurrentDomain.BaseDirectory + @"Resources\map\world.idx";

            ShapeFileFeatureLayer worldLayer = new ShapeFileFeatureLayer(sMapShapeFileName, sMapShapeIndexFileName);
            worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Country1;
            worldLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle = LineStyles.InternationalBorder1;
            worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

            Proj4Projection proj4 = new Proj4Projection();
            proj4.InternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(4326);
            proj4.ExternalProjectionParametersString = Proj4Projection.GetGoogleMapParametersString();

            worldLayer.FeatureSource.Projection = proj4;

            LayerOverlay staticOverlay = new LayerOverlay();
            staticOverlay.Layers.Add("WorldLayer", worldLayer);
            // staticOverlay.WrappingMode = WrappingMode.WrapDateline;
            wpfMap1.Overlays.Add(staticOverlay);


            ManagedProj4Projection p = new ManagedProj4Projection();
            p.InternalProjectionParametersString = ManagedProj4Projection.GetEpsgParametersString(4326);
            p.ExternalProjectionParametersString = ManagedProj4Projection.GetGoogleMapParametersString();
            p.Open();

            GraticuleAdornmentLayer graticuleAdornmentLayer = new GraticuleAdornmentLayer();
            graticuleAdornmentLayer.GraticuleLineStyle = LineStyles.DegreeLine1;
            // graticuleAdornmentLayer.WrappingMode = WrappingMode.WrapDateline;
            graticuleAdornmentLayer.Projection = p;

            wpfMap1.AdornmentOverlay.Layers.Add("graticule", graticuleAdornmentLayer);

            worldLayer.Open();

            //works
            wpfMap1.CurrentExtent = ExtentHelper.ZoomToScale(wpfMap1.ZoomLevelSet.ZoomLevel04.Scale, worldLayer.GetBoundingBox(), GeographyUnit.Meter, (float)this.ActualWidth, (float)this.ActualHeight);

            //error is thrown
            //wpfMap1.CurrentExtent = ExtentHelper.ZoomToScale(wpfMap1.ZoomLevelSet.ZoomLevel04.Scale, worldLayer.GetBoundingBox(), GeographyUnit.Meter, (float)this.ActualWidth, (float)this.ActualHeight);

            worldLayer.Close();

            wpfMap1.Refresh();

        }
 


Vicky, 
  
   You are absolutely right!  Thank you so much for the code, I was able to plug it in, test, and resolve the issue.  The problem was that it does support wrapping but if it sees that the X or Y unprojected, in decimal degrees, goes outside 180 or -180 then it thinks it is wrapping and then looked for this WrappingExtent.  If you are not wrapping then the extent is null and that is when all the fun starts.  So first off if you turn off wrapping then if should work after you get the fix.  If you turn wrapping on then you need to set the GraticuleAdornmentLayer.WrappingExtent property and set the extent of the world map, so it know where to start wrapping.  The build is most likely running now so it might be too later to get it into today’s build but you can check version 5.5.76.0 in the developers build.  If it doesn’t work there, not using wrapping, then wait until Friday morning and grab 5.5.77.0. 
  
 PS: Sorry that I didn’t realize that it might work in wrapping mode.  I don’t work on that part of the system often and my laziness took over. 
  
 David

David,


Thank you for your reply. I will test it tomorrow and let you know how it goes.


Thanks,

Vicky



Hi David,


It is working fine now.


Thanks,

Vicky


 


 



Hello Vicky, 
  
 I’m glad it’s working now, please feel free to let us know if you have questions. 
  
 Regards, 
  
 Gary

The NullReferenceException is designed as a valid runtime condition that
can be thrown and caught in normal program flow. It indicates that you
are trying to access member fields, or function types, on an object
reference that points to null. That means the reference to an Object
which is not initialized. More about…NullPointerException

Wlash

Hi Wlash,

Thanks for your share, have you met the exception when use our map?

Regards,

Don