C# 使用 Visual Studio 设计器 - “未将对象引用设置为对象的实例”(Visual Studio 2008)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1033177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using the Visual Studio designer - "Object reference not set to an instance of an object" (Visual Studio 2008)
提问by Kivus
I recently got tossed some C# code to make some minor cosmetic changes to. When I open up some of the files in Visual Studio though, I get errors saying:
我最近扔了一些 C# 代码来对它进行一些细微的外观更改。但是,当我在 Visual Studio 中打开一些文件时,我收到错误消息:
To prevent possible data loss...
为防止可能的数据丢失...
The first of these errors is:
这些错误中的第一个是:
Object reference not set to an instance of an object.
你调用的对象是空的。
I can follow the stack trace fine, but I'm not sure what I really should be looking for in this situation. Also, the end of my stack trace has a call that ends in "PageScroller..ctor()".
我可以很好地跟踪堆栈跟踪,但我不确定在这种情况下我真正应该寻找什么。此外,我的堆栈跟踪的末尾有一个以“PageScroller..ctor()”结尾的调用。
Based on a little Google research, I'm assuming that means call the constructor. Is that true?
根据谷歌的一些研究,我假设这意味着调用构造函数。真的吗?
采纳答案by John Saunders
You have a bug in design mode for some custom control, probably PageScroller, and apparently starting from the constructor. Perhaps there's some code in the constructor that returns null in design mode, and the null is not checked for.
您在某些自定义控件的设计模式中存在错误,可能是 PageScroller,并且显然是从构造函数开始的。也许构造函数中有一些代码在设计模式下返回 null,并且没有检查 null。
回答by jon37
I occasionally see problems like this. I started moving code from the constructor to the load event and that helped.
我偶尔会看到这样的问题。我开始将代码从构造函数移动到加载事件,这有所帮助。
回答by Eric Nelson
Make sure that the auto generated
确保自动生成
InitializeComponent();
method is called before trying to reference any object created in designer.
在尝试引用设计器中创建的任何对象之前调用方法。