Message 1 of 5
System.NullReferenceException ERROR

Not applicable
08-27-2007
02:29 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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
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