title: Quartz - .NET Core - Null Reference Exception
body: <p>I'm trying to implement the Quartz for .NET Core.
  At the moment I have the following:</p>
  
  <pre><code>    public class Startup
          {
              **private IScheduler _scheduler { get; set; }//quartz**
              public Startup(IConfiguration configuration)
              {
                  Configuration = configuration;
                  **_scheduler = ConfigureQuartz();//quartz**
              }
  
              public IConfiguration Configuration { get; }
  
              public void ConfigureServices(IServiceCollection services)
              {
                  services.AddDbContext&lt;Context&gt;(options =&gt;
                  {
                  options.UseSqlServer(Configuration.GetConnectionString("notebook14"));
                  });
  
  ...
  ...
  ...
  
                  **#region for Quartz DI
                  services.AddScoped&lt;Job&gt;();
                  services.AddSingleton(provider =&gt; _scheduler);
                  #endregion**
  
              }
  
              public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
              {
                  if (env.IsDevelopment())
                  {
                      app.UseDeveloperExceptionPage();
                  }
                  else
                  {
                      app.UseExceptionHandler("/Home/Error");
                      app.UseHsts();
                  }
                  app.UseHttpsRedirection();
                  app.UseStaticFiles();
  
                  app.UseRouting();
  
                  app.UseAuthentication();
                  app.UseAuthorization();
  
                  app.UseEndpoints(endpoints =&gt;
                  {
  
                      endpoints.MapControllers();
                      endpoints.MapRazorPages();
  
                   endpoints.MapControllerRoute(
                      name: "default",
                      pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
                  });
  
              }
              **#region for Quartz
              private IScheduler ConfigureQuartz()
              {
  
                  NameValueCollection properties = new NameValueCollection()
                  {
                      {"quartz.serializer.type","binary"}
                  };
                  StdSchedulerFactory factory = new StdSchedulerFactory(properties);
                  _scheduler = factory.GetScheduler().Result;
                  _scheduler.Start();
  
                  return _scheduler;
              }
  
              #endregion**
  </code></pre>
  
  <p>...and Program.cs because I would like to start a schedule at first deploy and then run the task every day:</p>
  
  <pre><code>public class Program
      {
          public static void Main(string[] args)
          {
              var host = CreateHostBuilder(args).Build();
              using (var scope = host.Services.CreateScope())
              {
                  var services = scope.ServiceProvider;
                  UserManager&lt;AppUser&gt; userManager = services.GetRequiredService&lt;UserManager&lt;AppUser&gt;&gt;();
                  RoleManager&lt;AppRole&gt; roleManager = services.GetRequiredService&lt;RoleManager&lt;AppRole&gt;&gt;();
  
                  **IScheduler scheduler = services.GetRequiredService&lt;IScheduler&gt;();
                  Start.StartSchedule(scheduler);**
  
                  Init.AssignAdmin(userManager, roleManager).Wait();
              }
              host.Run();
  
          }
  
          public static IHostBuilder CreateHostBuilder(string[] args) =&gt;
              Host.CreateDefaultBuilder(args)
                  .ConfigureWebHostDefaults(webBuilder =&gt;
                  {
                      webBuilder.UseStartup&lt;Startup&gt;();
                  });
      }
  </code></pre>
  
  <p>...and Start.cs, where I have created the job and the trigger :</p>
  
  <pre><code> public class Start
      {
          public static Context _context { get; set; }
          public Start(Context context)
          {
              _context = context;
          }
          public async static void StartSchedule(IScheduler _scheduler)
          {
              IJobDetail job = JobBuilder.Create&lt;Job&gt;().WithIdentity("Generate", "FreeSlots").Build();
  
              ITrigger trigger = TriggerBuilder.Create().WithIdentity("Trigger", "FreeSlots").StartNow()
                  .WithDailyTimeIntervalSchedule(t=&gt;t.StartingDailyAt(new TimeOfDay(10,59)))
                  .Build();
  
              await _scheduler.ScheduleJob(job, trigger);
          }
      }
  </code></pre>
  
  <p>...and finally, the Job itself: </p>
  
  <pre><code>public class Job : IJob
      {
          public Task Execute(IJobExecutionContext context)
          {
              try
              {
                  Console.WriteLine("TRIGGERED!");
                  List&lt;AppUser&gt; doctors = Start._context.Users.Where(x =&gt; x.Type == AppUser.UserType.Doctor).ToList();
  
                  DateTime First = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 8, 0, 0);
  
                  List&lt;Appointments&gt; appointments = new List&lt;Appointments&gt;()
              {
                  new Appointments(){Date=First, Time=First,Status=Appointments.SlotStatus.Free},
                  new Appointments(){Date=First, Time=First.AddHours(1),Status=Appointments.SlotStatus.Free},
                  new Appointments(){Date=First, Time=First.AddHours(2),Status=Appointments.SlotStatus.Free},
                  new Appointments(){Date=First, Time=First.AddHours(3),Status=Appointments.SlotStatus.Free},
                  new Appointments(){Date=First, Time=First.AddHours(4),Status=Appointments.SlotStatus.Free},
                  new Appointments(){Date=First, Time=First.AddHours(5),Status=Appointments.SlotStatus.Free},
                  new Appointments(){Date=First, Time=First.AddHours(6),Status=Appointments.SlotStatus.Free},
                  new Appointments(){Date=First, Time=First.AddHours(7),Status=Appointments.SlotStatus.Free},
                  new Appointments(){Date=First, Time=First.AddHours(8),Status=Appointments.SlotStatus.Free},
  
              };
  
                  foreach (AppUser doc in doctors)
                  {
                      foreach (Appointments slot in appointments)
                      {
                          slot.DoctorId = doc.Id;
                          Start._context.Appointments.Add(slot);
                          Start._context.SaveChanges();
                          var message = "Slot for " + slot.DoctorId.ToString() + " " + slot.Time + " " + slot.Status;
                          Console.WriteLine(message);
                      }
  
                  }
  
              }
              catch (Exception ex)
              {
                  Console.WriteLine(ex);
              }
  
              return Task.CompletedTask;
          }
      }
  </code></pre>
  
  <p>The problem is that I get a null reference exception when the Job is executed:
  <a href="https://i.stack.imgur.com/V4LyZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/V4LyZ.png" alt="enter image description here"></a></p>
  
  <p>Does anybody have any ideas? Is there a way to make this work? Thank you in advance!</p>