简体   繁体   中英

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). 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

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

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. When objID = 1 Or 2 Or 3, the function worked very well. But when objID = 4. the function exception.

What could be the reason? Issue in C# or Dll file?

Is there something error in the Instance() OR 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. And you are passing it into a method, the said array objID

Can you include a code sample of what you are trying to achieve?

It is not very clear.

What is ID? Has ID been instantiated with the new ? What is ObjGen?

Do you have the function 4 in your dll, if not that explains why you got a null reference exception. Double check your DLL and make sure you are calling the function 4.

Hope this helps you in giving you hints? Best regards, Tom.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM