Klemkas
Klemkas

Reputation: 107

Why one variable is initialized, but other is null?

I can't understand why one variable initialises ok and other is null, though both methods are described here

Surface (System.Drawing.Bitmap bitmap)

Create a SdlImage instance from a System.Drawing.Bitmap object. Loads a bitmap from a System.Drawing.Bitmap object, usually obtained from a resource.

Surface (int width, int height)

Create surface of a given width and height.

Code example:

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

Full code:

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: line18 on program.main is 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

error:

NullReferenceException was unhandled
Object reference not set to an instance of an object.
Troubleshooting tips:
Use the "new" keyword to create an object instance.. etc.

I found a more verbose stack trace:

   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()

I have created another question prior to this here, i researched some tutorials and examples which i found only few, and they also throw same exception, in the evening i will turn on my old laptop and check if those examples will work there, because i swear they did.

Upvotes: 3

Views: 279

Answers (1)

Kenned
Kenned

Reputation: 578

The library is open source, so you can go to Sourceforge and look up the code.

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

In line 204 you see that that the constructor that fails for you is just calling another

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

-and if you go to http://sourceforge.net/p/cs-sdl/code/HEAD/tree/trunk/SdlDotNet/src/Graphics/VideoInfo.cs you can see that BitsPerPixel is a shortcut to PixelFormat.BitsPerPixel and PixelFormat is some nasty stuff:

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

And VideFormat has a "IsInitialized" property. So my guess is that you need to initialize the VideoInfo class in some way.

Upvotes: 1

Related Questions