为什么一个变量被初始化,但是其他变量是null?

时间:2014-07-02 07:37:37

标签: c# sdl.net

我无法理解为什么一个变量初始化为ok而其他变量为null,尽管两种方法都被描述为here

  

Surface(System.Drawing.Bitmap位图)

     

从System.Drawing.Bitmap对象创建SdlImage实例。从System.Drawing.Bitmap对象加载位图,通常从资源获取。

     

Surface(int width,int height)

     

创建给定宽度和高度的表面。

代码示例:

        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);
    }
}

Stack Trace: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之前创建了另一个问题,我研究了一些教程和示例,我发现只有少数,他们也抛出相同的例外,晚上我会打开我的旧笔记本电脑并检查是否有例子会在那里工作,因为我发誓他们做了。

1 个答案:

答案 0 :(得分: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有一个&#34; IsInitialized&#34;属性。 所以我的猜测是你需要以某种方式初始化VideoInfo类。