TPECI

Hello,

I'm getting crazy with all of these error in my codes.

this time a faced with a unhandeled exception.

code:

hr = g_pJoystick11->SetDataFormat(&c_dfDIJoystick2);

definition of ( c_dfDIJoystick2 ) in the dinput.h :

: const DIDATAFORMAT c_dfDIJoystick2

Error:

An unhandled exception of type 'System.NullReferenceException' occurred in tre.exe

Additional information: Object reference not set to an instance of an object.

I am really confused with these errors.

please give me a way to escape from my codes errors !!!!!!!

Thank You, Hanif



Re: Visual C++ General Unhandled exeption

Simple Samples

I think that g_pJoystick11 is the Object reference that is not set to an instance of an object.




Re: Visual C++ General Unhandled exeption

TPECI

Hello,

But why it works in vc++6 but it do not work here.

please tell the solution what should i do .

an it give me some thread and places in assembly code but i do not know what to do with that





Re: Visual C++ General Unhandled exeption

einaros

System.NullReferenceException is something you'd get with managed code. VC++6 doesn't support managed code, so you seem to have got something mixed up.

Can you paste the initialization of the joystick object






Re: Visual C++ General Unhandled exeption

TPECI

ref class DirectInput

{

public:

LPDIRECTINPUTDEVICE8 g_pJoystick11;

...

}

..

pin_ptr<LPDIRECTINPUTDEVICE8> p = &g_pJoystick11;

LPDIRECTINPUTDEVICE8* ptr_pJoystick = p;

hr = g_pDI11->CreateDevice( pdidInstance->guidInstance, ptr_pJoystick, NULL);





Re: Visual C++ General Unhandled exeption

TPECI

Hi,

why nobody answer me, Please post me a solution.

don't tell me it's an exception with no solution. at leaste tell me these why these kind of exeptions happend.





Re: Visual C++ General Unhandled exeption

crescens2k

You are getting this exception because you are creating something and its failing. You should be checking the return values to see if it is giving you S_OK or a failure code. It is obviously failing at some point because you are getting a null pointer back so you should check hr to see what code it is giving. That will then give you the information you need as to why it is failing.




Re: Visual C++ General Unhandled exeption

Simple Samples

TPECI wrote:

Hi,

why nobody answer me, Please post me a solution.

don't tell me it's an exception with no solution. at leaste tell me these why these kind of exeptions happend.

We don't have enough information. Perhaps the only way anyone can help you is if they are psychic. Others have tried to help, but if you have tried the solutions and they don't work or if you don't know how to use the solutions then you need to understand that people are trying to help based on the imformation provided.

I said that probably g_pJoystick11 is null. If so, then we don't know what the source code is that is failing. If g_pJoystick11 is not null, then it would really help if you can tell us what is null and what is supposed to be creating the object.






Re: Visual C++ General Unhandled exeption

einaros

TPECI wrote:
Hi,

why nobody answer me, Please post me a solution.

don't tell me it's an exception with no solution. at leaste tell me these why these kind of exeptions happend.

My apologies, TPECI, I completely missed your response.

As crescens2k points out, some initializing code is either missing or failing. In such situations you should debug run your application, and check which variable isn't initialized. Once you've verified which variable is the culprit, review your source code and see that you've actually allocated the necessary resources and called the proper functions to init the data.






Re: Visual C++ General Unhandled exeption

TPECI

H,

Really thank you. this is all problematics part, i think the delegate that i have made is the problem, Please look at this:

Class and defenition of delegates:

ref class DirectInput

{

public:

delegate int EnumJoysticksDelegate(const DIDEVICEINSTANCE* pdidInstance,VOID* pContext);

delegate int EnumObjectsDelegate(const DIDEVICEOBJECTINSTANCE* pdidoi,VOID* pContext);

DirectInput(void);

LPDIRECTINPUT8 g_pDI11;

LPDIRECTINPUTDEVICE8 g_pJoystick11;

......

private:

/*Functions which delegate are made for*/

BOOL CALLBACK EnumObjectsCallback( const DIDEVICEOBJECTINSTANCE* pdidoi,VOID* pContext );

BOOL CALLBACK EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance,VOID* pContext );

}

HRESULT DirectInput::InitDirectInput(HWND hDlg)

{

HRESULT hr;

EnumJoysticksDelegate^D_Joy = gcnew EnumJoysticksDelegate(this,&DirectInput::EnumJoysticksCallback);

EnumObjectsDelegate^D_Obj = gcnew EnumObjectsDelegate(this,&DirectInput::EnumObjectsCallback);

pin_ptr<LPDIRECTINPUT8> p_g_pDI = &g_pDI11;

LPDIRECTINPUT8* ptr_pDI = p_g_pDI;

hr = DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION,

IID_IDirectInput8, (void**)ptr_pDI, NULL);

if FAILED(hr)

{

Error = TEXT("DirectInput8Create FAILED");

return hr;

}

hr = g_pDI11->EnumDevices( DI8DEVCLASS_GAMECTRL,

(LPDIENUMDEVICESCALLBACK)Marshal::GetFunctionPointerForDelegate(D_Joy).ToPointer(), /* I checked it and i got nothing will send to the function*/

NULL, DIEDFL_ATTACHEDONLY );

if ( FAILED(hr) )

{

Error = TEXT("EnumDevices Failed");

return hr;

}

hr = g_pJoystick11->SetDataFormat(&c_dfDIJoystick2);

if FAILED(hr)

{

Error = TEXT("SetDataFormat Failed");

return hr;

}

....

}

BOOL CALLBACK DirectInput::EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance,VOID* pContext )

{

UNREFERENCED_PARAMETER( pContext );

HRESULT hr;

pin_ptr<LPDIRECTINPUTDEVICE8> p = &g_pJoystick11;

LPDIRECTINPUTDEVICE8* ptr = p;

hr = g_pDI11->CreateDevice( pdidInstance->guidInstance, ptr , NULL);

if ( FAILED(hr) )

return DIENUM_CONTINUE;

return DIENUM_STOP;

}

now i hope you can help me.

Thank you, Hanif





Re: Visual C++ General Unhandled exeption

TPECI

Hi,

this is all related part of code to the error.

As I mentioned ,delegates are the problems , now what should i do to solve it.

hr = g_pDI11->EnumDevices( DI8DEVCLASS_GAMECTRL,

(LPDIENUMDEVICESCALLBACK)Marshal::GetFunctionPointerForDelegate(D_Joy).ToPointer(), NULL, DIEDFL_ATTACHEDONLY );

When code reach to this part it should goto :

BOOL CALLBACK DirectInput::EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance,VOID* pContext )

but it don't go. why it don't work.

Thank You, Hanif Nouri