Error "Object reference not defined for an object instance."

Asked

Viewed 468 times

0

I am developing a Windows service that installs normal, but every time the start, it accuses the following error:

Pcservice Error on: 15/12/2016 10:53:13 Undefined object reference for an object instance.
in Datalayer.Repository.Configrobotrepository.Findbyname(String name) in RSPC.PCService.Scheduleservice() at line 0

I’ve reviewed the code and done several tests, but I can’t understand which object it’s not finding. Follow 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 must I do to correct this mistake ?

  • Debug and find out which line the error occurs.

  • You have checked whether the config instance of the Configrobot class is receiving an object through the Configrobotcontroller.Findbyname(schedular. Key)?

  • Guy debugs the code and sees which object is null. In foearch can be the "schedulars", it can be the openSession() method that is not working and consequently not populating the Session and when you will use the Session gives dick. Several possibilities...

  • NHibernateHelper it was you who created, right?

  • @jbueno yes, it is the class that configures Nhibernate

  • And that method OpenSession() works? You use it elsewhere?

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.