web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

How to catch or handle a null object reference

(0) ShareShare
ReportReport
Posted on by 172

HI,

I'm looking for a way to catch "Object reference  not set to an instane of an object" error. 

As I'm working with DocuRef files on my Dev machine I don't have access to  the files that I did not store myself on my machine but were stored in Production database and when I try to get the Attachment Stream it gives me the error and my code ends.

I would like to know a way to proceed my code skipping just the method that gives me the error.

    private void getDocuRefFile(RecId _docuRef)
    {
        DocuRef docuReflocal = docuRef::findRecId(_docuRef);
        
        if(docuReflocal.RecId)
        {
            try
            {
            System.IO.Stream stream = DocumentManagement::getAttachmentStream(docuReflocal);
            }
            catch(Exception::Error)
            {
                
            }
        }
    }

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    It'll be better if you can prevent the exception. Debug the code to see where the exception occurs and think about what you can do with it.

    If it's not possible, use the following code to catch it:

    System.NullReferenceException nullRefEx;
    
    try
    {
        ...
    }
    catch (nullRefEx)
    {
    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 592 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 478 Super User 2025 Season 2

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 305 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans