The most likely cause is that you have set the D3D11_CREATE_DEVICE_DEBUG
creation flag but your system does not have the Direct3D Debug Device installed. Running the legacy DirectX SDK doesn't work on Windows 7 SP1 with DirectX 11.1 or later to get the developer runtime.
As noted in the comments above, the HRESULT
value you get back from this function would have given a hint as to the cause, but you failed to output it in your code snippet.
The way you do get the debug device on your system depends on exactly which OS you are running. See this post for a full list. If you are running Windows 10, for example, no SDK installs it and instead you must enable the Graphics Tools "Feature On Demand".
Other possible causes here would be:
There's no device on your system that supports your requestedLevels
list of Direct3D Hardware Feature levels
You have passed some invalid set of input parameters to either the device or swap chain creation--another reason to use the more modern approach of creating the swap chain independent of the device creation.
Note that D3D11CreateDeviceAndSwapChain
is a legacy function, so likely whatever material you are using as a tutorial is also outdated. See Anatomy of Direct3D 11 Create Device
If you are new to DirectX development, I recommend taking a look at the DirectX Tool Kit tutorials.