我目前正在制作一个基本的绘图程序.其中一个要求是能够保存绘制对象列表并将其重新加载.到目前为止,我编写了一个保存函数,将列表中的所有项目导出为XML格式,我目前正在研究装载部分.
只要程序遇到"<RechthoekTool>"(RectangleTool的荷兰语),它就会执行以下代码:
//Create new tool.
RechthoekTool tool = new RechthoekTool();
//Turn <Startpoint> and <Endpoint> into actual points
var sp = Regex.Replace(xn["Startpunt"].InnerText, @"[\{\}a-zA-Z=]", "").Split(',');
tool.startpunt = new Point(int.Parse(sp[0]), int.Parse(sp[1]));
var ep = Regex.Replace(xn["Eindpunt"].InnerText, @"[\{\}a-zA-Z=]", "").Split(',');
tool.eindpunt = new Point(int.Parse(ep[0]), int.Parse(ep[1]));
//Set colour and width of brush
string kleur = xn["Dikte"].InnerText;
kleur.Replace(@"Color [", "");
kleur.Replace(@"]", "");
Color c = Color.FromName(kleur);
tool.kwastkleur = c;
tool.kwast = new SolidBrush(c);
tool.dikte = int.Parse(xn["Dikte"].InnerText);
//Add to list
s.listItems.Add(tool);
Run Code Online (Sandbox Code Playgroud)
每当我运行程序时,我得到'NullReferenceException未处理'错误("对象引用未设置为对象的实例.")at
s.listItems.Add(工具);
但是,我确实在开始时实例化了这个工具,不是吗?可能导致此错误的原因是什么?一些谷歌搜索告诉我这可能是因为我忘了分配一个房产,但据我所知,我已经把它们全部覆盖了......
非常感谢帮助.
该错误是由于s或未s.listItems实例化的.
在没有看到更多代码的情况下,很难知道哪个是null,但是您猜测是要创建一个新对象s,其中包含属性/字段listItems,但您没有为其分配列表listItems.
| 归档时间: |
|
| 查看次数: |
257 次 |
| 最近记录: |