locked
private static void Method called from outside of class RRS feed

  • Question

  • Hi,

    We have a class written as below:

    public static class BusyIndicatorService
     {
      
      private static bool _isBusy;
      
      public static void SetBusyState()
      {
       SetBusyState(true);
      }
      
      private static void SetBusyState(bool busy)
      {
       if (busy != _isBusy)
       {
        
        Mouse.OverrideCursor = busy ? Cursors.Wait : null;
        //bla bla bla
       }
      }
    }

    we are regularly getting below exception from production:

    System.NullReferenceException: Object reference not set to an instance of an object. at
    BusyIndicatorService.SetBusyState(Boolean busy) at
    ApplySchedule(Object input) at
    SaveSchedule(Object obj) at

    We are calling SetBusyState() from ApplySchedule method but stack trace is showing SetBusyState(Boolean busy) called which is actully private static void so that it cannot be called from out side of class.

    Why & how stack trace is showing a private static void Method being called?

    Wednesday, July 17, 2013 10:41 AM

Answers

  • "You asume that Mouse cannot be null, but do you know that it is not null?"

    That Mouse thing is the WPF Mouse class, of course that since it's a class it can't be null. And Cursors is also a static class.

    An exception in the static constructor is an interesting hypothesis but that normally generates a TypeInitializationException.

    It's quite strange to see a NullRefException thrown by that code. I'm left wondering if the stack trace isn't way more incomplete than it seems.

    I'd try to reproduce the exception with a debug build, that one should produce an accurate stack trace.

    • Marked as answer by Bob Shen Friday, July 26, 2013 7:40 AM
    Saturday, July 20, 2013 6:31 AM

All replies

  • "Why & how stack trace is showing a private static void Method being called?"

    That can happen if the JIT compiler inlines the SetBusyState() method.

    Wednesday, July 17, 2013 10:45 AM
  • Could also be that you somewhere reset the Stacktrace by accident:

    http://www.codeproject.com/Articles/9538/Exception-Handling-Best-Practices-in-NET#Don%27tclearthestacktracewhenre-throwinganexception20


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.

    Wednesday, July 17, 2013 11:33 AM
  • That would make the whole top (or bottom, depending how you look at it) of the stack disappear, it cannot somehow remove a single stack frame in the middle of the stack trace.
    Wednesday, July 17, 2013 11:46 AM
  • I'm not sure I understand your problem. In ApplySchedule() you say you are calling SetBusyState()  (with no arguments).  This is a valid call, since that method is public static.  That method in turn calls the private static SetBusyState(true) method (passing true for the argument).  That is perfectly valid.  The private method is not being called from outside the class, it's being called from inside the class, specifically from the SetBusyState() public method.
    Wednesday, July 17, 2013 9:38 PM
  • Chris, his issue is that SetBusyState() is not in the StackTrace.

    According to the StackTrace "private static void SetBusyState(bool busy)" is called directly from "ApplySchedule(Object input)" wich is not part of the class.

    And the only real explanation so far is that the JIT is inlining the call of SetBusyState(). It can do that without problem. Access modifiers are after all only there to prevent the programmer from doing something stupid/unwanted. They have no meaning in the compiled MSIL or how it is executed after the JIT is done with it.


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.

    Wednesday, July 17, 2013 10:21 PM
  • Hi

    Try to initialize the _isBusy to some value. Some times the Compiler will throw the warnings as errors

    Thursday, July 18, 2013 4:48 AM
  • Actual code in SetBusyState is as below. as per my understanding this method can not throw Object reference not set because Mouse(static class) can not be null, DispatcherPriority(enum) can not be null & Application.Current can not be null, dispatcherTimer_Tick is static method so it can not be null. Then how this method is throwing object reference not set?

    private static void SetBusyState(bool busy)
      {
       if (busy != _isBusy)
       {
        _isBusy = busy;
        Mouse.OverrideCursor = busy ? Cursors.Wait : null;
        if (_isBusy)
        {
         new DispatcherTimer(TimeSpan.FromSeconds(0), DispatcherPriority.ApplicationIdle, dispatcherTimer_Tick, Application.Current.Dispatcher);
        }
       }
      }
             
      private static void dispatcherTimer_Tick(object sender, EventArgs e)
      {
       var dispatcherTimer = sender as DispatcherTimer;
       if (dispatcherTimer != null)
       {
        SetBusyState(false);
        dispatcherTimer.Stop();
       }
      }

    source of above code is http://www.techques.com/question/1-10118788/MVVM-Wait-Cursor-how-to-set-the.wait-cursor-during-invocation-of-a-command

    Thursday, July 18, 2013 6:24 AM
  • You asume that Mouse cannot be null, but do you know that it is not null? Asumptions don't bring you anywhere, only knowledge does.
    What about a constructor? The Static Constrcutor of a Static Class could be executed long before the UI is instantiated.
    What about Cursors? If it is not an enum, then it has to be initialised (from the Windows Theme) as well.


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.

    Saturday, July 20, 2013 5:56 AM
  • "You asume that Mouse cannot be null, but do you know that it is not null?"

    That Mouse thing is the WPF Mouse class, of course that since it's a class it can't be null. And Cursors is also a static class.

    An exception in the static constructor is an interesting hypothesis but that normally generates a TypeInitializationException.

    It's quite strange to see a NullRefException thrown by that code. I'm left wondering if the stack trace isn't way more incomplete than it seems.

    I'd try to reproduce the exception with a debug build, that one should produce an accurate stack trace.

    • Marked as answer by Bob Shen Friday, July 26, 2013 7:40 AM
    Saturday, July 20, 2013 6:31 AM