Skip to main content
gamedev.net
🔒 Locked

[.net] NullReferenceException

Started by hypa_dude Mar 25, 2009 at 3:30 PM 4 replies 1k views
Original Post
hypa_dude
hypa_dude
I've written a Windows form application in Visual Studio. For some reason I am getting a NullReference Exception ("Object reference not set to an instance of an object") when I try to close the main form. Otherwise, the debug build works perfectly fine. Why is this? Is there a way to tell which field/variable/parameter is null and causing the exception? Below I've pasted the code where the exception occurs.


//program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Stimulation
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1()); //NullReferenceException
        }
    }
}



Mike.Popoloski
Mike.Popoloski
Look at the call stack for the exception. Exceptions typically contain a lot of extra info to help you track them down.
Mike Popoloski | Journal | SlimDX
Horup
Horup
I think an exception is thrown somewhere in the Form1 object.. Post your Form1 class and lets have a look, or try looking at your FormClosing or FormClosed events (If I remember correct) in Form1. If you have them, they might generate the exception when they are called when you close your form.
hypa_dude
hypa_dude
My Form 1 Class contains about 700 lines of code (to handle the nearly 50 controls I've put on it). I can post this if you wish, Will it fit in a single post? Will I irritate the moderator?

Also, I don't have any event handlers created for formclosing or formclosed. What should I look for in the call stack?
dmatter
dmatter
You can use [ source lang="c#" ][ /source ] tags (without the spaces) around your code to add a scrollable box that your code will appear within. You could also try stripping away code that you think has nothing to do with the problem to make it easier for people here to analyse. It will also help to post the callstack in those tags too.
hypa_dude
hypa_dude
Below I have pasted the Call Stack and the Form1 Class. Thanks for the help!

> Stimulation.exe!Stimulation.Program.Main() Line 18 C#
[Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) + 0x3a bytes
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() + 0x2b bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 0x66 bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x6f bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 bytes


*removed*


Edit/Update: I've solved the problem. I was using Mitov Videolab components and simply removed them from the forms designer. I'm not sure why these were causing the problem, since they weren't before. The documentation on these components is severly lacking.

[Edited by - hypa_dude on March 26, 2009 1:57:17 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.