托管 C++,未将对象引用设置为对象的实例

发布于 2024-12-07 08:38:33 字数 1074 浏览 1 评论 0原文

我以前遇到过这个问题,但从来没有遇到过这样的情况。我完全糊涂了。正如问题所述,我收到运行时错误“对象引用未设置为对象的实例”。使用调试器工具,我想我已经将问题定位到这一行:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

亣腦蒛氧 2024-12-14 08:38:33

您可能需要检查并确保您的 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.

演多会厌 2024-12-14 08:38:33

这是 C# 中的一个优点,而 C++/CLI 中却缺少这一点。 C# 生成的代码可确保 this 永远不会为 null。通过在方法上设置断点并检查“this”,可以在调试器中轻松查看。下面是一个重现异常的示例程序:

#include "stdafx.h"

using namespace System;

ref class Example {
    String^ dataFileLocation;
public:
    void SetPath(String^ path) { 
        dataFileLocation = path;   // Set breakpoint here and inspect "this"
    }
};

int main(array<System::String ^> ^args)
{
    Example^ obj /* = gcnew Example */;
    obj->SetPath("foo");
    return 0;
}

删除 /* */ 注释进行修复。通过查看调用堆栈来查找忘记实例化对象的方法来修复代码。

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:

#include "stdafx.h"

using namespace System;

ref class Example {
    String^ dataFileLocation;
public:
    void SetPath(String^ path) { 
        dataFileLocation = path;   // Set breakpoint here and inspect "this"
    }
};

int main(array<System::String ^> ^args)
{
    Example^ obj /* = gcnew Example */;
    obj->SetPath("foo");
    return 0;
}

Remove the /* */ comments to fix. Fix your code by looking at the call stack to find the method that forgot to instantiate the object.

草莓味的萝莉 2024-12-14 08:38:33

托管 C++ 使用 nullptr 来表示 null 引用。所以你可以检查:

if (path == nullptr) { ... }

或使用:

if (!String::IsNullOrEmpty(path))

Managed C++ uses nullptr for null references. So you can check:

if (path == nullptr) { ... }

or use:

if (!String::IsNullOrEmpty(path))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文