简体   繁体   English

System.NullReferenceException-对象引用未设置为对象的实例。 C#中的原因

[英]System.NullReferenceException - Object reference not set to an instance of an object. Causes in C#

Good day. 美好的一天。

I'm new to C# and trying to call functions from a DLL (C++ ObjGen). 我是C#的新手,正尝试从DLL(C ++ ObjGen)调用函数。 I'm getting this message and can't figure out why. 我收到此消息,不知道为什么。 Here's the code: 这是代码:

object[] objID = new object[] { ID };

ObjGen.Invoke(InvokeEnum.Query_State, objID);

Dll code dll代码

/////////////////////// ///////////////////////

INVOKE_FUNC_IMPLEMENT(Query_State, CUtil::GetState)

/////////////////////// ///////////////////////

bool CUtil::GetState(InvokeEnum methodID, SAFEARRAY * psa)
{
    UNIT_ID UID = (UNIT_ID)PARAM_LONG(0);

    CUtil::Instance()->NotifyUpdateState(UID);
    for(int slot = 0; slot < SLOT_ID_MAX; slot++)
        g_CManager.m_PJAssociArray[UID][slot] = 0;

    return true;
}

There is a loop called the top Invoke function. 有一个循环称为top Invoke函数。 When objID = 1 Or 2 Or 3, the function worked very well. 当objID = 1或2或3时,该功能运行良好。 But when objID = 4. the function exception. 但是当objID = 4时,函数异常。

What could be the reason? 可能是什么原因? Issue in C# or Dll file? 在C#或Dll文件中出现问题?

Is there something error in the Instance() OR NotifyUpdateState() ? Instance()或NotifyUpdateState()是否存在错误? i can't do the judgement. 我不能做判断。

thank you. 谢谢。

You are creating an array of objects in which an object of ID is one element in that array. 您正在创建一个对象数组,其中ID对象是该数组中的一个元素。 And you are passing it into a method, the said array objID 然后将其传递给方法,即数组objID

Can you include a code sample of what you are trying to achieve? 您是否可以包含要实现的代码示例?

It is not very clear. 这不是很清楚。

What is ID? 什么是ID? Has ID been instantiated with the new ? ID是否已使用new实例化? What is ObjGen? 什么是ObjGen?

Do you have the function 4 in your dll, if not that explains why you got a null reference exception. 您的dll中是否有函数4,如果没有,则说明您为什么会得到空引用异常。 Double check your DLL and make sure you are calling the function 4. 仔细检查您的DLL,并确保您正在调用函数4。

Hope this helps you in giving you hints? 希望这有助于您提供提示吗? Best regards, Tom. 最好的问候,汤姆。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# Npgsql 连接检查器异常抛出 System.NullReferenceException '对象引用未设置为 object 的实例。 - C# Npgsql connection checker Exception Throw System.NullReferenceException 'Object reference not set to an instance of an object.' System.NullReferenceException:“对象引用未设置为 object 的实例。” 问题 - System.NullReferenceException: „Object reference not set to an instance of an object.” problem System.NullReferenceException: '对象引用未设置为 object 的实例。 在处理通过 c# 编辑的 word 文档时 - System.NullReferenceException: 'Object reference not set to an instance of an object. while working with a word document to edit via c# 会话{“对象引用未设置为对象的实例。”} System.Exception {System.NullReferenceException} - session {“Object reference not set to an instance of an object.”} System.Exception {System.NullReferenceException} System.NullReferenceException:对象引用未设置为对象的实例。 抛出错误 - System.NullReferenceException: Object reference not set to an instance of an object. throwing error System.NullReferenceException:未将对象引用设置为对象的实例。 在测试自动化执行期间 - System.NullReferenceException: Object reference not set to an instance of an object. During Test automation execution System.NullReferenceException未将对象引用设置为对象的实例。 关于模型实例化 - 有时候 - System.NullReferenceException Object reference not set to an instance of an object. on Model instantiation - SOMETIMES “ System.NullReferenceException-对象引用未设置为对象的实例。” Web方法,用户控件 - “System.NullReferenceException— Object reference not set to an instance of an object.” Webmethod, User control MVC:通过SelectListItem,我得到“ System.NullReferenceException:对象引用未设置为对象的实例。”错误 - MVC: By SelectListItem I get “System.NullReferenceException: Object reference not set to an instance of an object.” error 获取 System.NullReferenceException:“对象引用未设置为 object 的实例。” 调用服务时 - getting System.NullReferenceException: 'Object reference not set to an instance of an object.' when calling a service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM