0

I have a service that checks other services and reports information on them.

The error is:

Object reference not set to an instance of an object. at ServiceControl.ServiceCore.CheckServiceStatus(List`1 myservices)

Code Block

protected void CheckServiceStatus(List<ServiceConfig> myservices)
    {
        if (!restarting)
        {
            for (int i = 0; i < myservices.Count; i++)
            {
                string emailBody = "";
                try
                {

                    if (myservices[i].service.Status == ServiceControllerStatus.Running)
                    {
                        serviceName = myservices[i].service.DisplayName;

                        try
                        {
                            foreach (ManagementObject obj in searcher.Get())
                            {
                                uint processId = (uint)obj["ProcessId"];
                                Process process = null;

                                process = Process.GetProcessById((int)processId);

                                //Assign memory to service for later use
                               myservices[i].memoryUsage = process.WorkingSet64;

                                //Check process memory vs threshold
                               if (myservices[i].memoryUsage > myservices[i].memoryThreshold)
                                {
                                    //report if memory exceeds threshold
                                    emailBody = myservices[i].serviceName + " has exceeded its memory threshold of " + myservices[i].memoryThreshold + "\n"
                                                + "Current memory usage: " + Convert.ToString(myservices[i].memoryUsage);
                                    EmailReport(emailBody, false);

                                    //extend memory threshold by %
                                    myservices[i].memoryThreshold *= myservices[i].memoryThresholdIncPercentage;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            EventLog.WriteEntry(ex.Message);
                            emailBody = "Could not find process for " + myservices[i].serviceName;
                            EmailReport(emailBody, true);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventLog.WriteEntry(ex.Message + "\n" + ex.StackTrace + "\n memory");
                }

                    try
                    {
                        if (myservices[i].service.Status != myservices[i].status)
                        {
                            //Report change in service, includes last saved memory
                            emailBody = myservices[i].serviceName + " has changed status" + "\n" +
                                        "from: " + Convert.ToString(myservices[i].status) + " to: " + Convert.ToString(myservices[i].service.Status) + "\n" +
                                        "Memory Usage: " + Convert.ToString(myservices[i].memoryUsage);
                            EmailReport(emailBody, false);
                        }
                    }
                    catch (Exception ex)
                    {
                        EventLog.WriteEntry(ex.Message + "\n" + ex.StackTrace + "\n status");
                    }

            }
        }




        if (lastDate != DateTime.Today)
        {
            restarting = true;
            RestartServices();
        }
    }

I have done a fair amount of debugging and my results are: myservices is not Null, it contains exactly 1 item in this instance.

I have also tried making a localized list within the block to consume myservices and work of the local list. this did not work.

I have tried passing myservices[i] to a local var and use the far, this did not work.

Any help would be much appreciated

lilGrim
  • 23
  • 2
  • 5
  • What line does the error fall on? – DGibbs Feb 20 '14 at 10:16
  • so... `myservices` is not null... that's your first thing to determine... but what about `myservices[0]`? `myservices[0].service`? `myservices[0].memoryUsage`? etc... why do you have exactly zero null checks??? – Code Jockey Feb 20 '14 at 16:27

1 Answers1

0

Every time, those errors mean that you're referencing something that isn't instantiated. Therefore if your myservices[i] instance is NOT NULL, a property you're trying to reference for it MUST be.

Have you checked all properties of the myservices[i] objext?

Jon Bellamy
  • 3,333
  • 20
  • 23