Jump to content
  • Advertisement
Sign in to follow this  
Graham

[.net] System.NullReferenceException in DX and C++

This topic is 4884 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

With the code below I get a runtime error and when I debug I get this information: An unhandled exception of type 'System.NullReferenceException' occurred in Engine.exe Additional information: Object reference not set to an instance of an object. I believe it is caused because I have run out of video memory but I dont even get a chance to see what hRet holds so I cant see the exact error. I believe its video memory because my work computer has a TNT Vanta and this error only happens when I start the application above a certain resolution (850X850 instead of 600X600). In debug mode the cursor is at the line with &m_pTex. What can I do to stop this exception. Thanks. edit. I suppose my question really is can I make a .Net form but not use the .Net exceptions and handle errors myself.
HRESULT hRet;
	m_width = 256;
	m_height = 256;

	hRet = D3DXCreateTextureFromFileEx( Graphics.m_pd3dDevice,
                                 "Terrain/ball.bmp",
                                 m_width, //D3DX_DEFAULT, // I had to set width manually. D3DPOOL_DEFAULT works for textures but causes problems for D3DXSPRITE.
                                 m_height, //D3DX_DEFAULT, // I had to set height manually. D3DPOOL_DEFAULT works for textures but causes problems for D3DXSPRITE.
                                 1,   // Don't create mip-maps when you plan on using D3DXSPRITE. It throws off the pixel math for sprite animation.
                                 D3DUSAGE_DYNAMIC, // usage
                                 D3DFMT_R8G8B8, //D3DFMT_UNKNOWN, // format
                                 D3DPOOL_DEFAULT, // pool
                                 D3DX_DEFAULT, // filter
                                 D3DX_DEFAULT, // mipfilter
                                 D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,1.0f), //colorkey
                                 NULL, // sourceinfo
                                 NULL, // pallete
                                 &m_pTex );

	if(FAILED(hRet))
	{
		debug << "Create surface failed: " << hRet << endl;
	}



[Edited by - Graham on June 1, 2005 10:59:04 AM]

Share this post


Link to post
Share on other sites
Advertisement
NullReferenceException means that you are trying to dereference a pointer which is null.

So with a debugger, check that no parameter is null when it's not supposed to be. Seeing your code, the only one worth checking is Graphics. I'm a bit confused as to why the debugger halts on the line &m_pTex, though...

The exception has to be thrown in your code, because D3DXCreateTextureFromFileEx is not a managed function, and as such it cannot throw a NullReferenceException. But just to be sure: check the call stack when the exception happens. Are you in your code, or in the D3DX code ?

To answer your other question: no there is no way to prevent .NET exceptions. They are 100% part of the design of the language, and you cannot ignore them. And I'm sure that it wouldn't help you anyway. Because if your call to D3DXCreate... does return a hRet, it means that it has not thrown any exception, and .NET does not do anything with the result, other than storing it in hRet.
The way to handle errors is to catch them with a try .. catch block, and to take appropriate measure there. The exception itself can contain additionnal informations as to what happened.

Out of curiosity: is there a reason that you use .NET, but not Managed Direct X, which would probably make such calls a bit easier ?

Good luck,
jods

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!