0

I found out this is because system reported error at wrong line. Problem solved.

System.NullReferenceException: Object reference not set to an instance of an object. at CheckRfqStatus.Program.StartCheckingRFQStatus(Int32 startDateVariable) in D:\PRIVACY\Program.cs:line 121

Line 121:

if (null == mapiFolderArchive.Folders["Sent Email to Vendor"]) { logger.Debug("9"); }

Here are the relevant code:

MAPIFolder mapiFolderArchive = (MAPIFolder)outlook.Session.Folders[archiveFolderName].Folders["Inbox"].Parent;
if (null == (MAPIFolder)outlook.Session.Folders[archiveFolderName]) { logger.Debug("33"); }
if (null == (MAPIFolder)outlook.Session.Folders[archiveFolderName].Folders["Inbox"]) { logger.Debug("35"); }
if (null == (MAPIFolder)outlook.Session.Folders[archiveFolderName].Folders["Inbox"].Parent) { logger.Debug("4"); }
MAPIFolder archiveSentEmailToVendorFolder = mapiFolderArchive.Folders["Sent Email to Vendor"];
if (null == mapiFolderArchive) { logger.Debug("7"); }
if (null == mapiFolderArchive.Folders) { logger.Debug("8"); }
//Below is line 121
if (null == mapiFolderArchive.Folders["Sent Email to Vendor"]) { logger.Debug("9"); }

My logger didn't log anything at all, why is line 121 still have null error?

Based on all the advices, I redo my code.

MAPIFolder mapiFolderArchive = (MAPIFolder)outlook.Session.Folders[archiveFolderName].Folders["Inbox"].Parent;
MAPIFolder defaultSentToVendorFolder = mapiFolderPurchase.Folders["Sent Email to Vendor"];
if (mapiFolderArchive.Folders != null)
{
    if (mapiFolderArchive.Folders["Sent Email to Vendor"] != null)
    {
        MAPIFolder archiveSentEmailToVendorFolder = mapiFolderArchive.Folders["Sent Email to Vendor"];
        //Now system said this line below got null reference exception error.
        if (archiveSentEmailToVendorFolder != null)

Why is if (archiveSentEmailToVendorFolder != null) got null reference exception when I already checked if (mapiFolderArchive.Folders["Sent Email to Vendor"] != null)?

Pop
  • 493
  • 4
  • 19
  • 1
    Are you sure `logger` is not null? – Wagner DosAnjos Nov 28 '17 at 01:57
  • @wdosanjos No, logger definitely is not null because I tested it and it can log something at the start of program. – Pop Nov 28 '17 at 01:59
  • If you believe that standard duplicate does not explain your case please provide clear [MCVE] where you actually check for nulls (and more importantly prevent access to properties of null objects rather than just logging some information). There is also no indication that "logger" actually going to log anything (as we the code for it is not provided in the post) – Alexei Levenkov Nov 28 '17 at 02:12
  • What happens if you use `var archiveSentEmailToVendorFolder = mapiFolderArchive?.Folders?["Sent Email to Vendor"]);` like I suggested below? If you do that, what is the value of `archiveSentEmailToVendorFolder`? – mjwills Nov 28 '17 at 03:37
  • @mjwills .Net 3.5 can use Null Conditional Operator? – Pop Nov 28 '17 at 03:55
  • Yes, if you use VS 2015+ @Pop. If you can't use that, use the first solution I posted. _If it still doesn't work, please post a screenshot of the exception occurring where the code is visible behind it._ – mjwills Nov 28 '17 at 04:01
  • Did the below code change fix it @Pop ? Any luck with the screenshot? – mjwills Nov 28 '17 at 07:58
  • @mjwills Sorry, this one is currently has lower priority in my tasks list, I will get right back once I clear my other task. Really appreciate your help. – Pop Nov 28 '17 at 08:11

1 Answers1

0
MAPIFolder archiveSentEmailToVendorFolder = mapiFolderArchive.Folders["Sent Email to Vendor"];
if (null == mapiFolderArchive) { logger.Debug("7"); }
if (null == mapiFolderArchive.Folders) { logger.Debug("8"); }
if (null == mapiFolderArchive.Folders["Sent Email to Vendor"]) { logger.Debug("9"); }

The issue is that you have done a null check incorrectly. The first line of code is prior to your null check. And even if we removed the first line -in the above code, if .Folders is null, for example, then "8" will be written and then the next line will throw NullReferenceException.

The code needs to be:

MAPIFolder archiveSentEmailToVendorFolder;
if (null == mapiFolderArchive) { logger.Debug("7"); }
else if (null == mapiFolderArchive.Folders) { logger.Debug("8"); }
else if (null == mapiFolderArchive.Folders["Sent Email to Vendor"]) { logger.Debug("9"); }
else { archiveSentEmailToVendorFolder = mapiFolderArchive.Folders["Sent Email to Vendor"] };

Also consider using the Null Conditional Operator to avoid these kinds of null related issues.

var archiveSentEmailToVendorFolder = mapiFolderArchive?.Folders?["Sent Email to Vendor"]);
mjwills
  • 21,750
  • 6
  • 36
  • 59