System.NullReferenceException ERROR

System.NullReferenceException ERROR

Anonymous
Not applicable
860 Views
4 Replies
Message 1 of 5

System.NullReferenceException ERROR

Anonymous
Not applicable
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.

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…..”

I am using Microsoft Visual Studio 2008 Bata2 with AutoCAD 2008 and 2007.

I would appreciate any comments.

Thanks,
Jason

Code:
Imports System.IO
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.ApplicationServices
Public Class Class1
Const DirectoryName As String = "c:\temp"
Public myWatcher As FileSystemWatcher

_
Public Sub Watch()
Try
WriteLine("Starting Watcher")
StartFileWatcher()
Catch ex As Exception
End Try
End Sub

_
Public Sub WatchOff()
Try
EndFileWatcher()
WriteLine("Ending Watcher")
Catch ex As Exception
End Try
End Sub

_
Public Sub Plot()
Dim ConfigInfoName As String = ""
Try
WriteLine("Starting Plot")
Catch ex As Exception
End Try
End Sub

Public Sub StartFileWatcher()
' Create the FileSystemWatcher
myWatcher = New FileSystemWatcher(DirectoryName)
Try
' Add delegates for the desired events
AddHandler myWatcher.Created, New _
FileSystemEventHandler(AddressOf myWatcher_Created)
AddHandler myWatcher.Changed, New _
FileSystemEventHandler(AddressOf myWatcher_Changed)
AddHandler myWatcher.Renamed, New _
RenamedEventHandler(AddressOf myWatcher_Renamed)
AddHandler myWatcher.Deleted, New _
FileSystemEventHandler(AddressOf myWatcher_Deleted)
' You must set EnableRaisingEvents to true in order for _
events to be received
myWatcher.EnableRaisingEvents = True
' Display instructions and wait for user to exit program
WriteLine("Make changes to the {0} directory" & _
DirectoryName)
WriteLine( _
"You will see events generated from those changes.")
WriteLine("Enter WatchOff to exit the program.")
Finally
End Try
End Sub

Public Sub EndFileWatcher()
Try
myWatcher.Dispose()
Catch
End Try
End Sub

' Event handlers respond to events raised by FileSystemEventHandler
Private Sub myWatcher_Changed(ByVal sender As Object, _
ByVal e As FileSystemEventArgs)
MsgBox("File system event: File {0} {1}" & e.FullPath & _
e.ChangeType)
'WriteLine("ChangedFile") ‘causes ERROR
End Sub

Private Sub myWatcher_Created(ByVal sender As Object, _
ByVal e As FileSystemEventArgs)
MsgBox("File system event: File {0} {1}" & e.FullPath & _
e.ChangeType)
'WriteLine("Created File") ‘causes ERROR
End Sub

Private Sub myWatcher_Renamed(ByVal sender As Object, _
ByVal e As RenamedEventArgs)
MsgBox("File system event: File {0} {1} to {2}" & _
e.OldFullPath & e.ChangeType & e.FullPath)
'WriteLine("Renamed File") ‘causes ERROR
End Sub

Private Sub myWatcher_Deleted(ByVal sender As Object, _
ByVal e As FileSystemEventArgs)
MsgBox("File system event: File {0} {1}" & _
e.FullPath & e.ChangeType)
'WriteLine("Deleted File") ‘causes ERROR
End Sub

Public Sub WriteLine(ByVal Text As String)
Try
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(vbNewLine)
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(Text)
Autodesk.AutoCAD.ApplicationServices.Application.UpdateScreen()
Catch
End Try
End Sub
End Class
0 Likes
861 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable
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
0 Likes
Message 3 of 5

Anonymous
Not applicable
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.

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.

Jason
0 Likes
Message 4 of 5

Anonymous
Not applicable
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?

Jason
0 Likes
Message 5 of 5

Anonymous
Not applicable
If I were you, I would spend a little more time
learning the basics of the language and APIs
you're trying to use.

You cannot find out what's wrong with your code
by covering up the errors generated when it runs,
and when you write code like this:

Try
' Do something here
Catch
End Try

You are doing exactly the same as 'On Error Resume
Next', which means that exceptions or errors in your
code are being supressed.

The null reference exception is a result of trying to
use a null object, most likely returned by a property
reference, without checking it first.

You need to learn how to take the steps needed to
find out what specific object or property is returning
null. You do that by breaking up your statements, so
you do not have complex property references like this:

Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.

The problem is that one of the properties is returning
null, so you need to find out which it is.

Based on the situation here, my guess is that it is the
MdiActiveDocument property of the DocumentCollection
that is returning null, because it is being referenced in
a foreign thread.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5703724@discussion.autodesk.com...
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?

Jason
0 Likes