I am having trouble with winform opening a drawing. The error I am getting says NullReferenceException was unhandled and is highlighting the pathway. any help is appreciated. Thanks
private void button2_Click(object sender, EventArgs e)
{
//Open Solidworks Drawing
ModelDoc2 swMod开发者_如何学Pythonel = default(ModelDoc2);
DocumentSpecification swDocSpecification = default(DocumentSpecification);
string sName = null;
long longstatus = 0;
long longwarnings = 0;
// Drawing document path and name
swDocSpecification = (DocumentSpecification)swApp.GetOpenDocSpec("C:\\location\\????.slddrw");//File Location
sName = swDocSpecification.FileName;
// Sheet name
swDocSpecification.SheetName = "BOM"; //Open to the BOM sheet
swDocSpecification.DocumentType = (int)swDocumentTypes_e.swDocDRAWING;
swDocSpecification.ReadOnly = true;
swDocSpecification.Silent = false;
// Open the specified sheet in the specified drawing document
swModel = swApp.OpenDoc7(swDocSpecification);
longstatus = swDocSpecification.Error;
longwarnings = swDocSpecification.Warning;
}
System.Diagnostics.Process.Start("explorer.exe c:\\");
this can help
There are two possibilities as to why you're getting the NullReferenceException
- swApp is null and calling anything inlcuding
GetOpenDocSpec
won't work - Something inside GetOpenDocSpec isn't written the way it supposed to, and its not doing the correct checking. And so its throwing a null exception
It should pretty easy to just check if swApp == null using your debugger. Using the autos or watch windows, hovering over the variable, ?swApp == null from the command window, etc.
精彩评论