在升级到 Unity 和 Visual Studio 之前,我有一个简单的代码块,在更新之后运行没有错误,但是完全相同的逻辑引发了NullReferenceException
.
调用时引发异常,BitMap.Save()
但我已验证所有输入都已适当实例化,并且我可以在调试期间检查所有关联对象并且无法识别任何空值。在这种情况下,这NullReferenceException
是非常模棱两可的,我怀疑它与 Unity 的更新特别相关。
请不要关闭重复的“什么是 NullReferenceException,我该如何解决? ”就像我的另一个一样。这似乎是一个内部问题
Bitmap
,我正在寻找有关如何解决此问题的具体指导。
Bitmap newBmp = new Bitmap(16, 16);
for (int i= 0; i< 16; i++)
{
for (int j= 0; j< 16; j++)
{
newBmp.SetPixel(i, j, Color.blue);
}
}
using (MemoryStream ms = new MemoryStream())
{
newBmp.Save(ms, ImageFormat.Bmp); // Null??
// do more stuff with ms if it didn't crap out
}
异常详细信息,提出于newBmp.Save()
注意:两者都不
newBmp
为ms
空,并且上面的逻辑已将每个像素设置为蓝色。
NullReferenceException: Object reference not set to an instance of an object
System.Drawing.ComIStreamMarshaler+ManagedToNativeWrapper..cctor () (at <556bd4f081384ff9b1658a316bdf6616>:0)
Rethrow as TypeInitializationException: The type initializer for 'ManagedToNativeWrapper' threw an exception.
System.Drawing.ComIStreamMarshaler.MarshalManagedToNative (System.Object managedObj) (at <556bd4f081384ff9b1658a316bdf6616>:0)
System.Drawing.Image.Save (System.IO.Stream stream, System.Drawing.Imaging.ImageCodecInfo encoder, System.Drawing.Imaging.EncoderParameters encoderParams) (at <556bd4f081384ff9b1658a316bdf6616>:0)
System.Drawing.Image.Save (System.IO.Stream stream, System.Drawing.Imaging.ImageFormat format) (at <556bd4f081384ff9b1658a316bdf6616>:0)
(wrapper remoting-invoke-with-check) System.Drawing.Image.Save(System.I