【问题标题】:Why one variable is initialized, but other is null?为什么一个变量被初始化,而另一个为空?
【发布时间】: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,所以获取源代码并查看预期内容,或者使用调试器逐步完成...

标签: c# sdl.net


【解决方案1】:

该库是开源的,所以你可以去 Sourceforge 查找代码。

http://sourceforge.net/p/cs-sdl/code/HEAD/tree/trunk/SdlDotNet/src/Graphics/Surface.cs

在第 204 行中,您看到失败的构造函数只是在调用另一个

public Surface(int width, int height) : this(width, height, VideoInfo.BitsPerPixel){ }

-如果你去http://sourceforge.net/p/cs-sdl/code/HEAD/tree/trunk/SdlDotNet/src/Graphics/VideoInfo.cs 您可以看到 BitsPerPixel 是 PixelFormat.BitsPerPixel 的快捷方式,而 PixelFormat 是一些讨厌的东西:

private static Sdl.SDL_PixelFormat PixelFormat
{
    get
    {
        return (Sdl.SDL_PixelFormat)
            Marshal.PtrToStructure(VideoInfoStruct.vfmt,
            typeof(Sdl.SDL_PixelFormat));
    }
}

而且 VideFormat 有一个“IsInitialized”属性。 所以我的猜测是你需要以某种方式初始化 VideoInfo 类。

【讨论】:

  • 哇,好重的东西。挖掘图形库源代码对我来说真的太难了。但是您提到了 VideoInfo 类。就在几分钟前,我在this great tutorial 的帮助下使用 sdl.net 查找了我的工作游戏,发现这条线有效,我可以在表面上绘制并对其进行 blit。 Surface surf2 = Video.CreateRgbSurface(20, 20, 32, 0, 0, 0, 0, true);我应该将您的答案标记为“答案”吗?因为它可能是一些 sdl.net 错误或细微差别。
  • 我不知道这是否是一个错误,或者您是否只需要以某种方式初始化 VideoInfo 类,但您找到了解决方法很好。我实际上是想展示在开源库中查找某些内容是多么容易。 :) 我对 SDL 库一无所知,我只花了几分钟。你已经在做艰难的事情了——开发应用程序和编写代码,所以当这样的事情出现时,不要害怕查看库代码。 :-)
猜你喜欢
  • 2022-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多