问题描述
出现了错误:未处理的“System.NullReferenceException”类型的异常出现在Stranded.exe中。其他信息:未将对象引用设置到对象的实例。为什么?高手们说说。这是有关一个directx的一个例子。提示出错的地方在:m_device->SetProperty(DIPROP_RANGE,&range.diph);/////////CGameController::CGameController(LPDIRECTINPUT8input,HWNDhwnd){//Savecopies.gThis=this;m_hwnd=hwnd;m_inputSystem=input;//Initializethegamecontroller.DIPROPRANGErange;DIDEVCAPScaps;m_inputSystem->EnumDevices(DI8DEVCLASS_GAMECTRL,(LPDIENUMDEVICESCALLBACK)gJSEnumDeviceCallBack,NULL,DIEDFL_ATTACHEDONLY);if(m_device){range.diph.dwSize=sizeof(DIPROPRANGE);range.diph.dwHeaderSize=sizeof(DIPROPHEADER);range.diph.dwHow=DIPH_BYOFFSET;range.lMin=-1000;range.lMax=1000;range.diph.dwObj=DIJOFS_X;m_device->SetProperty(DIPROP_RANGE,&range.diph);/////////出错的这个range.diph.dwObj=DIJOFS_Y;m_device->SetProperty(DIPROP_RANGE,&range.diph);if(SUCCEEDED(m_device->GetCapabilities(&caps)))m_numButtons=caps.dwButtons;elsem_numButtons=4;}}
解决方案
解决方案二:
debug一下,看看m_device是不是nullptr
解决方案三:
引用1楼hdt的回复:
debug一下,看看m_device是不是nullptr
编译的时候没有错误,运行的时候不正常。m_device应该不为空,因为前面有个判断:if(m_device),如果为空后面的应该执行不到吧
解决方案四:
听说是PC上没有游戏手柄时就会出现该异常(本人没有游戏手柄无法验证),但是我将输入引擎中游戏手柄相关的代码注释掉之后,该游戏是可以运行的
解决方案五:
if(m_device)//这里不为0就可以通过,
解决方案六:
在托管c++里面m_device有可能是nullptr我还没试过if(nullptr)是什么结果
解决方案七:
http://user.qzone.qq.com/397613266/blog/1326359474自己解决了