【发布时间】:2020-03-07 00:18:14
【问题描述】:
我正在尝试将 WinForm 中的值(元素 ID)传递回 Command.cs 文件,但出现错误:
System.NullReferenceException: Object reference not set to an instance of an object.
at BatchSheetMaker.Command.Execute(ExternalCommandData commandData, String& message, ElementSet elements)
我在这里关注 youtube 教程,它看起来相当简单直接,但返回到 Command.cs 是另一层复杂性。
我将 Command.cs 代码包装在 try/catch 块中,它只是告诉我有 nullReferenceException,但它并没有告诉我它发生在哪一行。我有looked around,但没有找到任何关于如何使调试显示错误行的提示。如果有人有任何其他指示,那会很有帮助。
Form1.cs
public partial class Form1 : System.Windows.Forms.Form
{
private UIApplication uiapp;
private UIDocument uidoc;
private Autodesk.Revit.ApplicationServices.Application app;
private Document doc;
private string myVal;
public string MyVal
{
get { return myVal; }
set { myVal = value; }
}
public Form1(ExternalCommandData commandData)
{
InitializeComponent();
uiapp = commandData.Application;
uidoc = uiapp.ActiveUIDocument;
app = uiapp.Application;
doc = uidoc.Document;
}
public delegate void delPassData(System.Windows.Forms.ComboBox text);
private void Form1_Load(object sender, EventArgs e)
{
//Create a filter to get all the title block types.
FilteredElementCollector colTitleBlocks = new FilteredElementCollector(doc);
colTitleBlocks.OfCategory(BuiltInCategory.OST_TitleBlocks);
colTitleBlocks.WhereElementIsElementType();
foreach(Element x in colTitleBlocks)
{
comboBox1TitleBlockList.Items.Add(x.Name);
}
}
private void button1Continue_Click(object sender, EventArgs e)
{
MyVal = comboBox1TitleBlockList.Text;
}
Command.cs
Form1 form1 = new Form1(commandData);
String elementString = form1.MyVal.ToString();
Element eFromString = doc.GetElement(elementString);
ElementId titleBlockId = eFromString.Id;
ViewSheet sheet = ViewSheet.Create(doc, titleBlockId);
【问题讨论】:
-
如果你只是注释掉你的 try/catch 的东西,调试器就会出现在有问题的行上。
-
很公平。我原以为会有更聪明的方法来仍然有一个 try/catch 并让你的程序告诉你它在 try catch 中失败的地方而不会使程序崩溃
标签: c# winforms revit-api revit