Hi,

I have voidmain() function in Program.cs

[STAThread]
        static void Main(string[] args)
        {  


            Application.EnableVisualStyles();

        Application.SetCompatibleTextRenderingDefault(false);

        frmMain main = new frmMain();

        MainTool.frmmain = main;                   


        if (args != null && args.Length > 0)
        {

            Application.Run(new frmMain(args[0].ToString()));

        }

        else
        {
            Application.Run(main);
        }                 
    }      


 In that: MainTool.frmmain = main;     

Assign frmmain variable of MainTool class (data processing class) with frmMain of Form Main.

The reason to do so is because I have a class data processing Excel file: MainTool.cs in the project declare frmmain variables for calling formMain.

The problem will not happen if I do not make the registration program on Windows Context Menu. To do that, I must take the path of the file into the program. Like this:

            if (args != null && args.Length > 0)
            {

                Application.Run(new frmMain(args[0].ToString()));

            }
            else
            {
                Application.Run(main);
            }

If so, I still miss main variables above. When you debug programs appear error: object reference not set to an instance of an object. Because not set args[0] for the main variables.

How to assign?

Thanks!

Dani AI

Generated

The NullReferenceException here is almost always a symptom of a mismatch between the frmMain instance you assign to MainTool.frmmain and the instance you actually show when command-line args are present. creates an initial form and stores it in MainTool.frmmain, but when a second frmMain is constructed to accept the file path the static still points at the first instance (or nothing). If the form or MainTool code tries to use that static before it points at the same, visible form, a null reference or wrong-object behavior follows. It can also happen if either constructor reads that static too early.

Concrete, low-risk options to fix this

  • Create exactly one frmMain instance, initialize it with the file path (via constructor parameter or a setter) and then run that single instance so the static and visible form match.
  • If you must construct different instances, immediately set MainTool.frmmain to the exact instance you will run (and do that before any code that uses the static runs).
  • Avoid referencing MainTool.frmmain from inside frmMain constructors; move that work into OnLoad/Shown or an explicit Initialize method that you call after assigning the static.
  • Prefer removing the static-form dependency entirely: pass a MainTool instance or an interface into the form (dependency injection) so UI/logic are decoupled and easier to test.

Quick debugging checklist

  • Turn on breaking for NullReferenceException and inspect the call stack to see the exact line that fails.
  • Put breakpoints in the frmMain constructor and Load/Shown handlers to see when MainTool.frmmain gets used vs when it gets assigned.
  • Log the object identity (GetHashCode or reference) to confirm which instance is being referenced at runtime.

As pointed out, the simplest immediate fix is to ensure the one visible form is also the one referenced by MainTool; longer term, remove the global static and pass dependencies explicitly to prevent this class of bug.

Recommended Answers

All 3 Replies

The args parameter to the Main function is a list of command line parameters. If I run the following command from a cmd prompt:

MyApplication.exe -e 5

Then args[0] will be set to "-e" and args[1] will be set to "5". Note that the arguments are strings already (since args is a string[]) so there is no need to convert them using ToString() method.

Hi darkagn,

So, how to solve with error: "object reference not set to an instance of an object" that I encountered?

You need to work out exactly which object is yet to be initialised by stepping through your code with the debugger. The code you have posted looks correct, I am guessing the error is actually occurring in the constructor for frmMain, or maybe that MainTool.frmmain is not set to the same instance of frmMain.

Application.Run(new frmMain(args[0].ToString()));

The above line of code creates a new instance of frmMain, so MainTool.frmmain is no longer pointing to the right place. A better way of doing it would be to create a string property in your frmMain class called Path and doing the following:

frmMain main = new frmMain();
MainTool.frmmain = main;
if (args != null && args.Length > 0)
    main.Path = args[0];
Application.Run(main);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.