托管 C++,未将对象引用设置为对象的实例
我以前遇到过这个问题,但从来没有遇到过这样的情况。我完全糊涂了。正如问题所述,我收到运行时错误“对象引用未设置为对象的实例”。使用调试器工具,我想我已经将问题定位到这一行:
dataFileLocation = path;
整个函数在这里:
void DATReader::SetPath(String^ path)
{
if(!File::Exists(path))
{
MessageBox::Show( "DATReader (missing dat file: \n"+path+"\n )", "Error", MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
return;
}
dataFileLocation = path;
}
dataFileLocation
在这里声明,但没有分配给它:
ref class DATReader
{
private:
System::String^ dataFileLocation;
// ...
}
现在我知道我的原因了出现错误是因为 dataFileLocation
没有分配任何内容。但我在分配它时遇到问题。当我向其中添加 = 0;
时,它不会构建,因为它是一个 ref 类。当我尝试在构造函数中将其分配给 = 0;
时,它对我大喊大叫,因为我试图将其从 System::String^
转换为 int
。如果我将它分配给 = gcnew String("");
它会构建,但会抛出相同的运行时异常。
我不明白,我是否读错了调试器,而这根本不是问题的根源?我最近刚刚开始使用托管代码,所以我很困惑:\
I've run into this problem before, but never in a situation like this. I'm completely confused. As the question states, I'm getting the runtime error "Object reference not set to an instance of an object." Using the debugger tools, I think I've pinpointed the problem to this line:
dataFileLocation = path;
The entire function is here:
void DATReader::SetPath(String^ path)
{
if(!File::Exists(path))
{
MessageBox::Show( "DATReader (missing dat file: \n"+path+"\n )", "Error", MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
return;
}
dataFileLocation = path;
}
dataFileLocation
is declared here, but nothing is assigned to it:
ref class DATReader
{
private:
System::String^ dataFileLocation;
// ...
}
Now I know the reason I'm getting the error is because dataFileLocation
is assigned to nothing. But I'm having problems assigning it. When I add = 0;
to it, it won't build because its a ref class. When I try to assigned it to = 0;
in the constructor, it yells at me for trying to convert it from a System::String^
to an int
. If I assign it to a = gcnew String("");
it builds, but throws the same runtime exception.
I don't get it, am I reading the debugger wrong, and this isn't the source of the problem at all? I've just started to use managed code recently, so I'm confused :\
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能需要检查并确保您的 DATReader 对象也不为 null,它可能会在 DATReader.SetPath() 调用时引发异常。
You may want to check and make sure your DATReader object isn't null as well It may be throwing the exception at your DATReader.SetPath() call.
这是 C# 中的一个优点,而 C++/CLI 中却缺少这一点。 C# 生成的代码可确保 this 永远不会为 null。通过在方法上设置断点并检查“this”,可以在调试器中轻松查看。下面是一个重现异常的示例程序:
删除 /* */ 注释进行修复。通过查看调用堆栈来查找忘记实例化对象的方法来修复代码。
This is a nicety in C# that's missing in C++/CLI. C# generates code that ensures this can never be null. Easily seen in the debugger by setting a breakpoint on the method and inspecting "this". Here's an example program that reproduces the exception:
Remove the /* */ comments to fix. Fix your code by looking at the call stack to find the method that forgot to instantiate the object.
托管 C++ 使用
nullptr
来表示 null 引用。所以你可以检查:或使用:
Managed C++ uses
nullptr
for null references. So you can check:or use: