-3

How is it possible that thrown TimeoutException object is null and it throws

Object reference not set to an instance of an object.

in following line:

writeToLog(e2.ToString());

Check out this code.

WebServiceRef.CallResponse callResponse = null;
try
{
    callResponse = webServiceClient.Call(callRequest);
}
catch (TimeoutException e)
{
    try
    {
        WebServiceRef.CallStatusResponse callStatusResponse = webServiceClient.CallStatus(callStatusRequest);
        if (callStatusResponse.ResponseCode != 0)
        {
            throw new Exception("nok: " + callResponse.ResponseCode);
        }
    }
    catch (TimeoutException e2)
    {
        writeToLog(e2.ToString());
    }
}  

This is my writeToLog method.

private static void writeToLog(String logMsg)
{
    using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"log.txt", true))
    {
        file.WriteLine(DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss ") + logMsg);
    }
}

Stacktrace and message is this:

Object reference not set to an instance of an object.
 at ...(...) in c:\...cs:line 82
 at ...(...) in c:\...cs:line 193

Line 82 is point at

writeToLog(e2.ToString());

fritz
  • 111
  • 1
  • 11
  • 2
    It can't and it isn't. Do you have the full stack of the exception and are your debugging symbols up to date? (Clean & rebuild if you're not sure.) Verify that `writeLog` isn't the method that's throwing a `NullReferenceException`. – Jeroen Mostert Feb 24 '15 at 13:10
  • @fritz It isn't possible to throw a null exception and therefore it is not possible to catch a null exception - so the code that you are showing us can't be the actual code. – Matthew Watson Feb 24 '15 at 13:12
  • 1
    Are you sure this is the line of code that is throwing the exception? Try turning on _Debug -> Exceptions -> "Common Language Runtime Exceptions" -> Thrown_ and re-running the code. – Richard Ev Feb 24 '15 at 13:14
  • @MatthewWatson, yes, thank you. That's correct. Please note that Call and CallStatus is auto-generated code for SOAP webservice using ServiceReference. So it's really not my code which throws TimeoutException. – fritz Feb 24 '15 at 13:19
  • 1
    Given `writeToLog`, it is now a certainty that the `NullReferenceException` you get is not produced on the line that you think it is. Double check that your debug symbols are not out of date -- your line numbers aren't matching up. Rewrite the code if necessary -- note that it is extremely dubious to issue a call to a web service that you have just determined you can't reach due to a timeout! – Jeroen Mostert Feb 24 '15 at 13:23
  • Ok, thanks, I'll check that again. I'll be back :) – fritz Feb 24 '15 at 13:27
  • Replace writeToLog(e2.ToString()); with Debug.WriteLine(e2.ToString()); and I bet e2 is not null. – paparazzo Feb 24 '15 at 13:45
  • Yes, you were right. Thank you. There was a problem in line down further ... But, what's troubling me - .pdb file is up 2 date so how could application print incorrect line number? :( – fritz Feb 25 '15 at 11:36

1 Answers1

0

There is no way a null Exception instance can be thrown / caught.

Either something must be wrong with your debug symbols or you're not running the correct program. Try logging some test strings here and there to make sure the correct code is executed:

//...
catch (TimeoutException e2)
{
    Debug.WriteLine("If you don't see this in the output window then somehow you are not running this app.");
    writeToLog(e2.ToString());
}
//...
Crono
  • 10,211
  • 6
  • 43
  • 75
  • Yes, thank you. There was a problem in line below... But, what's troubling me - .pdb and .exe files are up 2 date so how could application print incorrect line number? :( – fritz Feb 25 '15 at 12:25
  • @fritz I'm afraid there's no easy way to diagnose this from my side. You may want to delete both files and build your project again, though. – Crono Feb 25 '15 at 13:15