<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to Python Scripting: How to detect and handle "Object reference not set to an instance of an object" error</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/dea45b2b17/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/Engineering/thread/dea45b2b17/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/Engineering/thread/dea45b2b17/</id><updated>2023-05-08T21:34:34.580000Z</updated><subtitle>Recent posts to Python Scripting: How to detect and handle "Object reference not set to an instance of an object" error</subtitle><entry><title>Python Scripting: How to detect and handle "Object reference not set to an instance of an object" error</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/dea45b2b17/?limit=25#85fd" rel="alternate"/><published>2023-05-08T21:34:34.580000Z</published><updated>2023-05-08T21:34:34.580000Z</updated><author><name>ckh1</name><uri>https://forge.codesys.com/u/ckhassan/</uri></author><id>https://forge.codesys.comb18bddac0cf0a85d55d443f12238cecad8173657</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hello,&lt;br/&gt;
Here's the scenario:&lt;br/&gt;
1. A for loop in a python script is used to cycle through and print out the name of the Library References in a python script using the Library Manager "references" property.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;If a Library Reference.is_managed property is true, and if the version of that library that is set in the Library Manager is not installed on the machine, the the script fails with "Object reference not set to an instance of an object." &lt;/li&gt;
&lt;/ol&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="err"&gt;code&lt;/span&gt;
&lt;span class="err"&gt;#This is an excerpt from the code.&lt;/span&gt;
&lt;span class="err"&gt;#The error occurs when trying to access displayname property on the last line.&lt;/span&gt;
&lt;span class="err"&gt;libraryReferences = applicationLevel_scriptObject.references&lt;/span&gt;
&lt;span class="err"&gt;for libraryReference in libraryReferences:&lt;/span&gt;
&lt;span class="err"&gt;    if libraryReference.is_managed:&lt;/span&gt;
&lt;span class="err"&gt;        print("-&amp;gt; %s" % (libraryReference.managed_library.displayname) ) #print the names of managed library references&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;How can I detect this error, so that I can handle it gracefully in my script?&lt;/p&gt;
&lt;p&gt;I've tried checking the follwoing, but none of them works:&lt;br/&gt;
libraryReference != NULL&lt;br/&gt;
libraryReference != Empty&lt;br/&gt;
isinstance(libraryReference, ScriptLibraryReference)&lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>