Nano HE
Nano HE

Reputation: 9307

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.

Upvotes: 0

Views: 531

Answers (1)

t0mm13b
t0mm13b

Reputation: 34592

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.

Upvotes: 1

Related Questions