<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: System.NullReferenceException ERROR in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/system-nullreferenceexception-error/m-p/2054376#M77192</link>
    <description>It is not really the sub routine itself, but when the sub routine tries to access AutoCAD executed by the file watcher.

Message was edited by: jluker</description>
    <pubDate>Mon, 27 Aug 2007 21:39:24 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2007-08-27T21:39:24Z</dc:date>
    <item>
      <title>System.NullReferenceException ERROR</title>
      <link>https://forums.autodesk.com/t5/net-forum/system-nullreferenceexception-error/m-p/2054375#M77191</link>
      <description>I have an error that I don’t know how to correct.  I activated a file event handler execute a sub routine.  While the file watcher is activated, I can run other routines via CommandMethod but when the file event handler is triggered, it pulls an error while trying to execute a sub routine.&lt;BR /&gt;
&lt;BR /&gt;
To make it simple, I am using a sub routine called WriteLine which writes a line at the command prompt.  I can activate this command by using CommandMethod but can’t with the event handler sub routine.  The error which I am getting is “"A first chance exception of type 'System.NullReferenceException' occurred in…..”&lt;BR /&gt;
&lt;BR /&gt;
I am using Microsoft Visual Studio 2008 Bata2 with AutoCAD 2008 and 2007.&lt;BR /&gt;
&lt;BR /&gt;
I would appreciate any comments.&lt;BR /&gt;
&lt;BR /&gt;
Thanks,&lt;BR /&gt;
Jason&lt;BR /&gt;
&lt;BR /&gt;
Code:&lt;BR /&gt;
Imports System.IO&lt;BR /&gt;
Imports Autodesk.AutoCAD.Runtime&lt;BR /&gt;
Imports Autodesk.AutoCAD&lt;BR /&gt;
Imports Autodesk.AutoCAD.DatabaseServices&lt;BR /&gt;
Imports Autodesk.AutoCAD.ApplicationServices&lt;BR /&gt;
Public Class Class1&lt;BR /&gt;
    Const DirectoryName As String = "c:\temp"&lt;BR /&gt;
    Public myWatcher As FileSystemWatcher&lt;BR /&gt;
&lt;BR /&gt;
    &lt;COMMANDMETHOD&gt; _&lt;BR /&gt;
    Public Sub Watch()&lt;BR /&gt;
        Try&lt;BR /&gt;
            WriteLine("Starting Watcher")&lt;BR /&gt;
            StartFileWatcher()&lt;BR /&gt;
        Catch ex As Exception&lt;BR /&gt;
        End Try&lt;BR /&gt;
    End Sub&lt;BR /&gt;
&lt;BR /&gt;
    &lt;COMMANDMETHOD&gt; _&lt;BR /&gt;
    Public Sub WatchOff()&lt;BR /&gt;
        Try&lt;BR /&gt;
            EndFileWatcher()&lt;BR /&gt;
            WriteLine("Ending Watcher")&lt;BR /&gt;
        Catch ex As Exception&lt;BR /&gt;
        End Try&lt;BR /&gt;
    End Sub&lt;BR /&gt;
&lt;BR /&gt;
    &lt;COMMANDMETHOD&gt; _&lt;BR /&gt;
    Public Sub Plot()&lt;BR /&gt;
        Dim ConfigInfoName As String = ""&lt;BR /&gt;
        Try&lt;BR /&gt;
            WriteLine("Starting Plot")&lt;BR /&gt;
        Catch ex As Exception&lt;BR /&gt;
        End Try&lt;BR /&gt;
    End Sub&lt;BR /&gt;
&lt;BR /&gt;
    Public Sub StartFileWatcher()&lt;BR /&gt;
        ' Create the FileSystemWatcher&lt;BR /&gt;
        myWatcher = New FileSystemWatcher(DirectoryName)&lt;BR /&gt;
        Try&lt;BR /&gt;
            ' Add delegates for the desired events&lt;BR /&gt;
            AddHandler myWatcher.Created, New _&lt;BR /&gt;
     FileSystemEventHandler(AddressOf myWatcher_Created)&lt;BR /&gt;
            AddHandler myWatcher.Changed, New _&lt;BR /&gt;
                 FileSystemEventHandler(AddressOf myWatcher_Changed)&lt;BR /&gt;
            AddHandler myWatcher.Renamed, New _&lt;BR /&gt;
                 RenamedEventHandler(AddressOf myWatcher_Renamed)&lt;BR /&gt;
            AddHandler myWatcher.Deleted, New _&lt;BR /&gt;
                 FileSystemEventHandler(AddressOf myWatcher_Deleted)&lt;BR /&gt;
            ' You must set EnableRaisingEvents to true in order for _&lt;BR /&gt;
              events to be received&lt;BR /&gt;
            myWatcher.EnableRaisingEvents = True&lt;BR /&gt;
            ' Display instructions and wait for user to exit program&lt;BR /&gt;
            WriteLine("Make changes to the {0} directory" &amp;amp; _&lt;BR /&gt;
                      DirectoryName)&lt;BR /&gt;
            WriteLine( _&lt;BR /&gt;
                  "You will see events generated from those changes.")&lt;BR /&gt;
            WriteLine("Enter WatchOff to exit the program.")&lt;BR /&gt;
        Finally&lt;BR /&gt;
        End Try&lt;BR /&gt;
    End Sub&lt;BR /&gt;
&lt;BR /&gt;
    Public Sub EndFileWatcher()&lt;BR /&gt;
        Try&lt;BR /&gt;
            myWatcher.Dispose()&lt;BR /&gt;
        Catch&lt;BR /&gt;
        End Try&lt;BR /&gt;
    End Sub&lt;BR /&gt;
&lt;BR /&gt;
    ' Event handlers respond to events raised by FileSystemEventHandler&lt;BR /&gt;
    Private Sub myWatcher_Changed(ByVal sender As Object, _&lt;BR /&gt;
                                  ByVal e As FileSystemEventArgs)&lt;BR /&gt;
        MsgBox("File system event: File {0} {1}" &amp;amp; e.FullPath &amp;amp; _&lt;BR /&gt;
               e.ChangeType)&lt;BR /&gt;
        'WriteLine("ChangedFile") ‘causes ERROR&lt;BR /&gt;
    End Sub&lt;BR /&gt;
&lt;BR /&gt;
    Private Sub myWatcher_Created(ByVal sender As Object, _&lt;BR /&gt;
                                  ByVal e As FileSystemEventArgs)&lt;BR /&gt;
        MsgBox("File system event: File {0} {1}" &amp;amp; e.FullPath &amp;amp; _&lt;BR /&gt;
               e.ChangeType)&lt;BR /&gt;
        'WriteLine("Created File") ‘causes ERROR&lt;BR /&gt;
    End Sub&lt;BR /&gt;
&lt;BR /&gt;
    Private Sub myWatcher_Renamed(ByVal sender As Object, _&lt;BR /&gt;
                                 ByVal e As RenamedEventArgs)&lt;BR /&gt;
        MsgBox("File system event: File {0} {1} to {2}" &amp;amp; _&lt;BR /&gt;
                e.OldFullPath &amp;amp; e.ChangeType &amp;amp; e.FullPath)&lt;BR /&gt;
        'WriteLine("Renamed File")  ‘causes ERROR&lt;BR /&gt;
    End Sub&lt;BR /&gt;
&lt;BR /&gt;
    Private Sub myWatcher_Deleted(ByVal sender As Object, _&lt;BR /&gt;
                                  ByVal e As FileSystemEventArgs)&lt;BR /&gt;
        MsgBox("File system event: File {0} {1}" &amp;amp; _&lt;BR /&gt;
               e.FullPath &amp;amp; e.ChangeType)&lt;BR /&gt;
        'WriteLine("Deleted File")   ‘causes ERROR&lt;BR /&gt;
    End Sub&lt;BR /&gt;
&lt;BR /&gt;
    Public Sub WriteLine(ByVal Text As String)&lt;BR /&gt;
        Try&lt;BR /&gt;
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(vbNewLine)&lt;BR /&gt;
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(Text)&lt;BR /&gt;
Autodesk.AutoCAD.ApplicationServices.Application.UpdateScreen()&lt;BR /&gt;
        Catch&lt;BR /&gt;
        End Try&lt;BR /&gt;
    End Sub&lt;BR /&gt;
End Class&lt;/COMMANDMETHOD&gt;&lt;/COMMANDMETHOD&gt;&lt;/COMMANDMETHOD&gt;</description>
      <pubDate>Mon, 27 Aug 2007 21:29:51 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/system-nullreferenceexception-error/m-p/2054375#M77191</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2007-08-27T21:29:51Z</dc:date>
    </item>
    <item>
      <title>Re: System.NullReferenceException ERROR</title>
      <link>https://forums.autodesk.com/t5/net-forum/system-nullreferenceexception-error/m-p/2054376#M77192</link>
      <description>It is not really the sub routine itself, but when the sub routine tries to access AutoCAD executed by the file watcher.

Message was edited by: jluker</description>
      <pubDate>Mon, 27 Aug 2007 21:39:24 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/system-nullreferenceexception-error/m-p/2054376#M77192</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2007-08-27T21:39:24Z</dc:date>
    </item>
    <item>
      <title>Re: System.NullReferenceException ERROR</title>
      <link>https://forums.autodesk.com/t5/net-forum/system-nullreferenceexception-error/m-p/2054377#M77193</link>
      <description>I have found that this error also occurs when someone is running a loop and has transactions within the loop. They could fix it by taking the transactions out of the loop and placing them at the end.&lt;BR /&gt;
&lt;BR /&gt;
This might have something to do with my problem because my file event reactor is running while trying to access AutoCAD. I have tried to clear the reactor before going into the sub routine but this does not help.&lt;BR /&gt;
&lt;BR /&gt;
Jason</description>
      <pubDate>Tue, 28 Aug 2007 19:25:50 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/system-nullreferenceexception-error/m-p/2054377#M77193</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2007-08-28T19:25:50Z</dc:date>
    </item>
    <item>
      <title>Re: System.NullReferenceException ERROR</title>
      <link>https://forums.autodesk.com/t5/net-forum/system-nullreferenceexception-error/m-p/2054378#M77194</link>
      <description>I have read that when you use a file watcher with a form, you have to SynchronizingObject to the form (.SynchronizingObject = Me).  I was wondering if this is my problem. If so, how could synchronize it to AutoCAD?&lt;BR /&gt;
&lt;BR /&gt;
Jason</description>
      <pubDate>Tue, 28 Aug 2007 22:12:26 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/system-nullreferenceexception-error/m-p/2054378#M77194</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2007-08-28T22:12:26Z</dc:date>
    </item>
    <item>
      <title>Re: System.NullReferenceException ERROR</title>
      <link>https://forums.autodesk.com/t5/net-forum/system-nullreferenceexception-error/m-p/2054379#M77195</link>
      <description>If I were you, I would spend a little more time &lt;BR /&gt;
learning the basics of the language and APIs&lt;BR /&gt;
you're trying to use.&lt;BR /&gt;
&lt;BR /&gt;
You cannot find out what's wrong with your code&lt;BR /&gt;
by covering up the errors generated when it runs,&lt;BR /&gt;
and when you write code like this:&lt;BR /&gt;
&lt;BR /&gt;
    Try&lt;BR /&gt;
          ' Do something here&lt;BR /&gt;
    Catch&lt;BR /&gt;
    End Try&lt;BR /&gt;
&lt;BR /&gt;
You are doing exactly the same as 'On Error Resume &lt;BR /&gt;
Next', which means that exceptions or errors in your &lt;BR /&gt;
code are being supressed.&lt;BR /&gt;
&lt;BR /&gt;
The null reference exception is a result of trying to&lt;BR /&gt;
use a null object, most likely returned by a property&lt;BR /&gt;
reference, without checking it first.&lt;BR /&gt;
&lt;BR /&gt;
You need to learn how to take the steps needed to&lt;BR /&gt;
find out what specific object or property is returning &lt;BR /&gt;
null. You do that by breaking up your statements, so &lt;BR /&gt;
you do not have complex property references like this:&lt;BR /&gt;
&lt;BR /&gt;
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.&lt;BR /&gt;
&lt;BR /&gt;
The problem is that one of the properties is returning&lt;BR /&gt;
null, so you need to find out which it is.&lt;BR /&gt;
   &lt;BR /&gt;
Based on the situation here, my guess is that it is the &lt;BR /&gt;
MdiActiveDocument property of the DocumentCollection &lt;BR /&gt;
that is returning null, because it is being referenced in&lt;BR /&gt;
a foreign thread.&lt;BR /&gt;
&lt;BR /&gt;
-- &lt;BR /&gt;
http://www.caddzone.com&lt;BR /&gt;
&lt;BR /&gt;
AcadXTabs: MDI Document Tabs for AutoCAD 2008&lt;BR /&gt;
Supporting AutoCAD 2000 through 2008&lt;BR /&gt;
http://www.acadxtabs.com&lt;BR /&gt;
&lt;BR /&gt;
&lt;JLUKER&gt; wrote in message news:5703724@discussion.autodesk.com...&lt;BR /&gt;
I have read that when you use a file watcher with a form, you have to SynchronizingObject to the form (.SynchronizingObject = Me).  I was wondering if this is my problem. If so, how could synchronize it to AutoCAD?&lt;BR /&gt;
&lt;BR /&gt;
Jason&lt;/JLUKER&gt;</description>
      <pubDate>Wed, 29 Aug 2007 17:47:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/system-nullreferenceexception-error/m-p/2054379#M77195</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2007-08-29T17:47:34Z</dc:date>
    </item>
  </channel>
</rss>

