.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

eNoDocument

2 REPLIES 2
Reply
Message 1 of 3
mbair
694 Views, 2 Replies

eNoDocument

After running the following command once, any subsequent calls to the command result in an error. Does anyone no why this would be? The first time the command runs, the oDocument variable is set without any problems, but any subsequent command executions error when trying to access the oDocument variable (e.g., oDatabase = oDocument.Database). The error states:

{"Object reference not set to an instance of an object."}

{code}
CommandMethod("xTest")> _
Public Sub xTest()
Dim oDocument As Document
Dim oDatabase As Database
Dim oTransaction As Transaction
Dim CommandString As String = ""
Try
oDocument = Application.DocumentManager.MdiActiveDocument
oDatabase = oDocument.Database
MsgBox(oDocument.Name, MsgBoxStyle.OkOnly, "FetzerAW")
'oTransaction = oDatabase.TransactionManager.StartTransaction
'Using oTransaction
' oTransaction.Commit()
'End Using
Catch ex As System.Exception
MsgBox(ex.Message, MsgBoxStyle.OkOnly, "FetzerAW")
Finally
If oTransaction IsNot Nothing Then oTransaction.Dispose()
If oDatabase IsNot Nothing Then oDatabase.Dispose()
If oDocument IsNot Nothing Then oDocument.Dispose()
End Try
End Sub
{code}

Edited by: mbair on Oct 14, 2009 11:21 AM
2 REPLIES 2
Message 2 of 3
norman.yuan
in reply to: mbair


Catch...
....
If oDatabase IsNot Nothing Then oDatabase.Dispose()
If oDocument IsNot Nothing Then oDocument.Dispose()
End Try



Why do you dispose the Document and Database objects that you did not create? You should not do that.

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 3
Anonymous
in reply to: mbair

One of the most difficult aspects of learning the AutoCAD .NET API, is the
confusion relating to IDisposable.Dispose().

Technically, calling Dispose() on any object that implements IDisposable
should do no harm and have no side-effects, and in most cases that holds
true in the AutoCAD API, with one notable exception, which is the Document
class.

You should *never* call Dispose() on a Document.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

wrote in message news:6271399@discussion.autodesk.com...
After running the following command once, any subsequent calls to the
command result in an error. Does anyone no why this would be? The first
time the command runs, the oDocument variable is set without any problems,
but any subsequent command executions error when trying to access the
oDocument variable (e.g., oDatabase = oDocument.Database). The error
states:

{"Object reference not set to an instance of an object."}

{code}
CommandMethod("xTest")> _
Public Sub xTest()
Dim oDocument As Document
Dim oDatabase As Database
Dim oTransaction As Transaction
Dim CommandString As String = ""
Try
oDocument = Application.DocumentManager.MdiActiveDocument
oDatabase = oDocument.Database
MsgBox(oDocument.Name, MsgBoxStyle.OkOnly, "FetzerAW")
'oTransaction =
oDatabase.TransactionManager.StartTransaction
'Using oTransaction
' oTransaction.Commit()
'End Using
Catch ex As System.Exception
MsgBox(ex.Message, MsgBoxStyle.OkOnly, "FetzerAW")
Finally
If oTransaction IsNot Nothing Then oTransaction.Dispose()
If oDatabase IsNot Nothing Then oDatabase.Dispose()
If oDocument IsNot Nothing Then oDocument.Dispose()
End Try
End Sub
{code}

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost