I'm developing a Windows service that installs normal, but every time I start it, it throws the following error:
PCService Error on: 12/15/2016 10:53:13 Object reference not set to an instance of an object.
in DataLayer.Repository.ConfigRobotRepository.FindByName (String name) in RSPC.PCService.ScheduleService () at line 0
I've already rolled the code and ran several tests, but I can not figure out which object it is not finding. Here are the codes:
ConfigRobotRepository.FindByName
public ConfigRobot FindByName(string name)
{
using (ISession session = NHibernateHelper.OpenSession())
{
ConfigRobot config = session.CreateCriteria(typeof(ConfigRobot))
.Add(Restrictions.Like("Name", name))
.UniqueResult<ConfigRobot>();
return config;
}
}
ScheduleService ()
try
{
foreach (var schedular in schedulars)
{
//Get from database mode from this key (example = "PregoeiroChama")
ConfigRobot config = ConfigRobotController.FindByName(schedular.Key);
DateTime scheduleTime = DateTime.MinValue;
if (config.Mode.ToUpper().Equals(ConfigRobotController.Interval))
{
//Get from database interval from this key (example = "PregoeiroChama)
int intervalMin = (int)config.IntervalMin;
scheduleTime = DateTime.Now.AddMinutes(intervalMin);
}
else if (config.Mode.ToUpper().Equals(ConfigRobotController.Daily))
{
//Get from database daily hour from this key (example = "PreogeiroChama")
int hour = (int)config.ScheduleTime;
if (DateTime.Now.Hour >= hour)
{
DateTime now = DateTime.Now.AddDays(1);
scheduleTime = new DateTime(now.Year, now.Month, now.Day, hour, 0, 0);
}
else
scheduleTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, hour, 0, 0);
}
TimeSpan timeSpan = scheduleTime.Subtract(DateTime.Now);
config.NextDate = scheduleTime;
config.Status = 'W';
/*Log*/
string schedule = string.Format("{0} day(s) {1} hours {2} minutes", timeSpan.Days, timeSpan.Hours, timeSpan.Minutes);
Log("PCService schedule " + config.Name + " to run after: " + schedule + " {0}", Path.GetTempPath() + config.Name + ".txt");
long dueTime = Convert.ToInt64(timeSpan.TotalMilliseconds);
schedular.Value.Change(dueTime, Timeout.Infinite);
ConfigRobotRepository repo = new ConfigRobotRepository();
repo.Update(config);
}
}
catch (Exception e)
{
System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(e, true);
var frame = trace.GetFrame(0);
var line = frame.GetFileLineNumber();
Log("PCService Error on: {0} " + e.Message + e.StackTrace + " at line " + line, Path.GetTempPath() + "PCService" + ".txt");
using (var service = new ServiceController("PCService"))
{
service.Stop();
}
}
What should I do to correct this error?