【发布时间】:2014-07-02 07:37:37
【问题描述】:
我不明白为什么一个变量初始化正常而另一个为空,尽管描述了这两种方法here
表面(System.Drawing.Bitmap 位图)
从 System.Drawing.Bitmap 对象创建一个 SdlImage 实例。从 System.Drawing.Bitmap 对象加载位图,通常从资源中获取。
表面(整数宽度,整数高度)
创建给定宽度和高度的表面。
代码示例:
Surface surf = new Surface((Bitmap)Bitmap.FromFile("example.png")); //this works {SdlDotNet.Graphics.Surface}
surfaceControl1.Blit(surf, new Point(0, 0));
surfaceControl1.Blit(surf, new Point(20, 20));
Surface surf2 = new Surface(20, 20); //this is {null} and throws exception
surf2.Fill(Color.White);
surfaceControl1.Blit(surf2);
完整代码:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void surfaceControl1_Click(object sender, EventArgs e)
{
Surface surf = new Surface((Bitmap)Bitmap.FromFile("example.png"));
surfaceControl1.Blit(surf, new Point(0, 0));
surfaceControl1.Blit(surf, new Point(20, 20));
//this works
Surface surf2 = new Surface(20, 20); //<-this throws null exception, details below
surf2.Fill(Color.White);
surfaceControl1.Blit(surf2);
}
}
堆栈跟踪:program.main 上的第 18 行是 Application.Run(new Form1());
sdl_control_surfaces_test.exe!WindowsFormsApplication1.Form1.surfaceControl1_Click(object sender, System.EventArgs e) Line 34
sdl_control_surfaces_test.exe!WindowsFormsApplication1.Program.Main() Line 18 + 0x1d bytes
错误:
NullReferenceException 未处理
对象引用未设置为对象的实例。
故障排除提示:
使用“new”关键字来创建一个对象实例……等等。
我发现了一个更详细的堆栈跟踪:
at SdlDotNet.Graphics.VideoInfo.get_VideoInfoStruct()
at SdlDotNet.Graphics.VideoInfo.get_PixelFormat()
at SdlDotNet.Graphics.Surface..ctor(Int32 width, Int32 height)
at WindowsFormsApplication1.Form1.surfaceControl1_Click(Object sender, EventArgs e) in C:\Users\Saska\documents\visual studio 2010\Projects\sdl_control_surfaces_test\sdl_control_surfaces_test\Form1.cs:line 34
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at WindowsFormsApplication1.Program.Main() in C:\Users\Saska\documents\visual studio 2010\Projects\sdl_control_surfaces_test\sdl_control_surfaces_test\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
我在here 之前创建了另一个问题,我研究了一些教程和示例,但发现很少,它们也抛出了同样的异常,晚上我会打开我的旧笔记本电脑并检查这些示例是否会在那里工作,因为我发誓他们做到了。
【问题讨论】:
-
Surface的构造函数是否会抛出在使用surf2之前正在处理的异常? -
它抛出的异常是什么。你能发布堆栈跟踪吗?
-
我更新了描述。 Surf 和 surf2 只用在这个按钮代码中,代码很简单。那么为什么“冲浪”有效,因为它们是相同的?会不会是 sdl.net 库有 bug?
-
也许是个愚蠢的问题......但是你怎么知道它是第二个用法而不是第一个用法?你能在他们周围抛出一个try catch(单独的)并发布他们捕获的异常吗?
-
这是 OSS,所以获取源代码并查看预期内容,或者使用调试器逐步完成...