-1

I have a class library, and I'm debugging it with a console app. This works perfectly.

The class is intended to be used with an existing Windows service on a server. I don't have a great deal of access to the server.

I update the service to utilize the class library. It's included in the class library in the solution, it's referenced, and it's included on the page.

However, when it's built on the server, it returns a null object.

The relevant service piece:

using MyNamespace;
...
private void TimerTick(){
    //this is called every minute
    EventLog.WriteEntry("myLog", "Start test", EventLogEntryType.Information);
    try
    {
        EventLog.WriteEntry("myLog", "I see this", EventLogEntryType.Information);
        MyClass test1 = new MyClass(); //Error occurs here
        EventLog.WriteEntry("myLog", "I never see this", EventLogEntryType.Information);
        test1.MyFunction();
    }
    catch (Exception e)
    {
        EventLog.WriteEntry("myLog", e.Message, EventLogEntryType.Information);
    }
    EventLog.WriteEntry("myLog", "Conclude test", EventLogEntryType.Information);
}

And the relevant class:

namespace MyNamespace
{
    public class MyClass
    {

        public SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString);
        public void MyFunction(){
            EventLog.WriteEntry("MyLog", "Get links", EventLogEntryType.Information);
        }
    }
}

In the event log I get the standard:

Object reference not set to an instance of an object.

There are no errors in the build and deploy on the server. The service runs as expected, it's just that null reference exception when trying to instantiate the class.

I'm fairly new to C#/.net

Is there a better way to go about troubleshooting this when the only error messaging I get is through the EventLog?

What are the possible causes of the class instantiation throwing a null reference exception?

Things I tried

First, used Exception.ToString() to get the stack trace, rather than Exception.Message like I initially did. This gave me great deal more information to debug with.

Second, looking at the class constructor itself for an error as a class instantiation cannot throw a null reference itself.

Randy Hall
  • 7,716
  • 16
  • 73
  • 151
  • 2
    Stop using `e.Message` use `e.ToString()` instead. You will have a lot more useful information about where the error is happening logged. As for your question of "*how do I go about troubleshooting this when the only error messaging I get is through the EventLog? What are the possible causes of the class being null?*" see the answers in the linked dupicate, they go over those exact things. – Scott Chamberlain Mar 29 '17 at 19:28
  • Try debugging and see if you hit the exception. It would be also helpful to see a stack trace – Akshay Mahajan Mar 29 '17 at 19:30
  • Does your `MyFunction()` method rely on any other properties that may potentially be null within a newly instantiated `MyClass` instance? This assumes that there aren't any issues with the `EventLog` instance itself (assuming it's the built-in one from `System.Diagnostics`) – Rion Williams Mar 29 '17 at 19:31
  • Please give more information like in which line the exception accured – itay_421 Mar 29 '17 at 19:31
  • How do you know that the error happens on the `//Error occurs here` line? You have nothing in your logs that shows you that is the problem line. Switch to `ToString()` instead of `e.Message` and see the actual place the error is happening in the logs via a stack trace. – Scott Chamberlain Mar 29 '17 at 19:34
  • @RionWilliams The EventLog is the built-in one. As for the function, I never manage to make it far enough to call it. See updated OP with error line marked – Randy Hall Mar 29 '17 at 19:36
  • @ScottChamberlain I get the log before that line, I don't get the log after that line. As for the *how do I go about troubleshooting this when the only error messaging I get is through the EventLog*, your comment to use `e.ToString()` answers that part. – Randy Hall Mar 29 '17 at 19:37
  • 1
    You need to update your answer with the the output of the e.ToString(). Right now, the code you have shown us ***can't*** throw that exception, there is nothing that is being referenced to be null. Either there is a constructor for `MyClass` you have not shown us, Static fields or static constructors you have not shown us, or there is other code being executed before or after the class that you have not shown us. – Scott Chamberlain Mar 29 '17 at 19:40
  • @ScottChamberlain So far you've provided two answers which both appear to be leading down the right path - use `ToString()` instead of `Message`, which has given me additional insight on that question you said is a duplicate that I already read and doesn't mention that even once, and that it would have to be something else inside the constructor. In this instance, it appears to be my database connection, which is a property on the class. I'm testing now and will update my question shortly – Randy Hall Mar 29 '17 at 19:54
  • @ScottChamberlain and it would be much appreciated if you would remove the duplicate mark. – Randy Hall Mar 29 '17 at 20:01
  • @RandyHall - I do not see any updated code that uses `e.ToString()` or the results of that message from the event log in your question. There is still nothing anyone can do to help you until you change that. – Igor Mar 29 '17 at 20:17
  • @RandyHall - what is needed is the entire Stack Trace which points to the line that causes the NRE. If you read through the duplicate link it will help you make more sense of this all, essentially its a guide to trouble shooting NREs. – Igor Mar 29 '17 at 20:19
  • @Igor I already solved the issue with direction from ScottChamberlain. The stack is mostly irrelevant, it was getting to the stack that I needed. Which should be the answer, but can't be because the question was closed. – Randy Hall Mar 29 '17 at 20:21
  • @ScottChamberlain can we please have the very specific question with the very specific answer that is not related to the duplicate mark reopened? – Randy Hall Apr 06 '17 at 14:54
  • Sure, post the solution – Scott Chamberlain Apr 06 '17 at 15:15

1 Answers1

0

The Exception.Message returns limited information. Better debug information can be logged by using Exception.ToString() when logging an error:

try
{
   //your code
}
catch (Exception e)
{
    EventLog.WriteEntry("myLog", e.ToString(), EventLogEntryType.Information);    
}

It's also not possible for a class instantiation to to throw a null reference exception - if you see one during an instantiation, something inside the class must be throwing an error. Using Exception.ToString() should provide the necessary information to identify the specific issue.

Randy Hall
  • 7,716
  • 16
  • 73
  • 151