TransactionManager.StartTransaction() throws NullReferenceException

TransactionManager.StartTransaction() throws NullReferenceException

adam.krug
Advocate Advocate
2,493 Views
7 Replies
Message 1 of 8

TransactionManager.StartTransaction() throws NullReferenceException

adam.krug
Advocate
Advocate

Inside of DB.Object_Modified event handler I'm examining the modified object. Sometimes I need to retrieve another object based on an ObjectId, thus I use transaction. 90% of times it works but sometimes TransactionManager.StartTransaction() method throws a NullReferenceExeption internally (stack trace below). Any idea how could I prevent this from happening (is there any property of DBObject, Document or anything else I could use to check if I can safely start a transaction?

 

System.NullReferenceException: Object reference not set to an instance of an object.
at Autodesk.AutoCAD.Runtime.Interop.CheckNull(IntPtr returnValue)
at Autodesk.AutoCAD.ApplicationServices.TransactionManager.StartTransaction()

 

Eventually I could use a try-catch block but I'm looking for a more efficient way.

0 Likes
2,494 Views
7 Replies
Replies (7)
Message 2 of 8

Virupaksha_aithal
Autodesk Support
Autodesk Support

Hi,

 

see if "StartOpenCloseTransaction" transaction helps in your case. refer https://adndevblog.typepad.com/autocad/2012/08/the-right-tools-for-the-job-autocad-part-5.html

 



Virupaksha Aithal KM
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 3 of 8

adam.krug
Advocate
Advocate

Hi @Virupaksha_aithal ,

In my case it didn't help but still thanks for pointing out on the StartOpenCloseTransaction , very interesting read!

 

I'm not sure what exactly is causing the problem, seems like the context for starting the transaction is sometimes not valid. Anyways, I decided to change my original approach: during Db_ObjectModified event handler I only add the modified objects on a list and examine them only after a command is finished, using Doc_CommandEnded event handler. Then I also clean the list. Works.

0 Likes
Message 4 of 8

timgrote
Enthusiast
Enthusiast

I've run into issues where there is no active document, and the code still runs from a palette or other event.

Checking for a null document has helped with that.

 

Document doc = CAD_App.DocumentManager.MdiActiveDocument;
if (doc == null) return;
0 Likes
Message 5 of 8

adam.krug
Advocate
Advocate

Hi @timgrote 

In my case the ActiveDoc is not null, I'm getting the null reference exception inside of the StartTransaction() call.

 

If ActiveDoc was null, I wouldn't be able to even reach StartTransaction() 

0 Likes
Message 6 of 8

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

does your active doc have a valid database? Otherwise it could be the AutoCAD start window.

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 7 of 8

_gile
Consultant
Consultant

Hi,

The NullReferenceException is probably related to an instance of TransactionManager or the instance of Database (or Document) the TransactionManager is got from.

Try simply doing:

 

using (var tr = new OpenCloseTransaction())
{
    // do your stuff with tr here
}

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 8 of 8

quan_hm
Collaborator
Collaborator

hi @adam.krug , 

Can you make more clear about your approach (or a sample code) about using Doc_CommandEnded event handler. I want to use that to modify object properties after a command finish. 

Hoang Minh Quan - Lead Developer

Autodesk Store for IntegaDesign Content

Smart Solutions by IntegaDesign

Plant 3D • CAD Optimization • Automation
0 Likes