[英]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.