1

If using Navigatiomanager null value exception came while using in Class file

NavigationManager navigationManager;
  public ICollection<TimeOff> GetTimeOffbyStaff(int id)
    {
      ICollection<TimeOff> timeOff = new List<TimeOff>();
      try
      {
        timeOff = labOrgDbContext.TimeOff.Include(x => x.Technologist).Where(x => x.TechnologistId == id && x.FromDate >= (DateTime.Now.AddYears(-1)).Date && x.IsDeleted != true).OrderByDescending(x => x.RowInsertOn).ToList();
      }
      catch (Exception ex)
      {
        ExceptionLogging.SendErrorToText(ex, "1");
        navigationManager.NavigateTo("/PagenotFound");
        throw ex;
      }
      return timeOff;
    }
jazb
  • 5,498
  • 6
  • 37
  • 44
novfal haq
  • 107
  • 2
  • 16
  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – jazb Nov 27 '19 at 06:20
  • 1
    also read: https://learn.microsoft.com/en-us/aspnet/core/blazor/dependency-injection?view=aspnetcore-3.0 – jazb Nov 27 '19 at 06:21
  • i do not understand your sentence - can you reword it? – jazb Nov 27 '19 at 06:35
  • Object reference not set to an instance of an object. error come on this line 'navigationManager.NavigateTo("/PagenotFound");' – novfal haq Nov 27 '19 at 06:47
  • 1
    have a look at `why` the object is `null` - clue...read my link on dependency injection above – jazb Nov 27 '19 at 06:49

1 Answers1

1

Refactor your code to not mix logic together with page navigation. Ie. have one class that will provide the "time off by staff" calculation. Then use that class in a visual page that will inject NavigationManager, and do the page navigation in case of error like this:

@page "/"
@inject NavigationManager navigationManager; // inject an instance of NavigationManager

<h1>Time-off by staff</h1>

Some page content.....

<button @onclick="GetTimeOffByStaff">Get time off by staff</button>

@code {
    void GetTimeOffByStaff()
    {
        try
        {
            TimeOffLogic logic = new TimeOffLogic();    // this will be your logic class

            var timeOff = logic.GetTimeOffbyStaff(id);
        }
        catch (Exception ex)
        {
            ExceptionLogging.SendErrorToText(ex, "1");
            navigationManager.NavigateTo("/PagenotFound");      // use NavigationManager
        }

        // do something with timeOff
    }
}
rk72
  • 976
  • 1
  • 10
  • 15
  • thanks Actually i want to redirect while exception happen in my class method due to that i try to do in class instead of component – novfal haq Nov 27 '19 at 06:50
  • @novfalhaq Then simply pass the `navigationManager` instance to the `TimeOffLogic` constructor, and then call it's `NavigateTo()` method. That will work too. – rk72 Nov 27 '19 at 11:39
  • NullReferenceException: Object reference not set to an instance of an object. private readonly NavigationManager navigationManager; public TaskSchedulerService(NavigationManager navigationManager,) { this.navigationManager = navigationManager; } – novfal haq Nov 27 '19 at 12:26
  • @novfalhaq How/when you inject the NavigatorManager? Did you use the same code as in the example above, ie. ` [Inject] public NavigationManager navigationManager { get; set; } `? If so, if you put a breakpoint on the call to the TaskSchedulerService constructor, are you seeing a non-null value of the navigationManager property? – rk72 Nov 27 '19 at 12:36