2017-11-28 2 views
0

잘못된 라인에서 시스템이 오류를보고했기 때문입니다. 문제 해결됨.모든 것이 null이 아니지만 시스템이 여전히 null 오류를 표시하도록했습니다.

ObjectReferenceException : 개체 참조가 개체의 인스턴스로 설정되지 않았습니다. D에 CheckRfqStatus.Program.StartCheckingRFQStatus (INT32 startDateVariable)에서 : PRIVACY \ Program.cs \ : 라인 121

선 121 :

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"); } 
: 여기

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

는 관련있는 코드

로거에서 아무 것도 로깅하지 않았는데도 왜 줄 121에 여전히 Null 오류가 있습니까?

모든 조언에 따라 코드를 다시 작성합니다. 이미 if (mapiFolderArchive.Folders["Sent Email to Vendor"] != null)을 선택한 경우

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) 

if (archiveSentEmailToVendorFolder != null)는 null 참조 예외가있어?

+1

'logger'가 null이 아닌가요? – wdosanjos

+0

@ wdosanjos 아니요, 로거는 확실히 테스트하지 않았기 때문에 null이 아니며 프로그램 시작시에 기록 할 수 있습니다. – Pop

+0

표준 복제가 사례를 설명하지 않는다고 생각하는 경우 실제로 null을 확인하는 곳에서 명확한 [MCVE]를 제공하십시오. 더 중요한 것은 일부 정보를 로깅하는 대신 null 객체의 속성에 대한 액세스를 차단하는 것입니다. "logger"가 실제로 아무것도 로그하지 않는다는 표시도 있습니다. (코드는 우리가 게시물에 제공하지 않았기 때문에) –

답변

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"); } 

문제는 null 검사를 잘못 수행 한 것입니다. 코드 첫 번째 줄은 이며, 앞에이 있고 null 수표입니다. 그리고 위의 코드에서 첫 번째 줄을 제거했다고하더라도 .Foldersnull 인 경우 "8"이 쓰여지고 다음 줄은 NullReferenceException이됩니다.

코드는 할 필요가 :

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"] }; 

또한 null 관련된 이러한 종류의 문제를 방지하기 위해 Null Conditional Operator을 사용하는 것이 좋습니다.

var archiveSentEmailToVendorFolder = mapiFolderArchive?.Folders?["Sent Email to Vendor"]); 
관련 문제