I'm having an error: object reference not set to an instance of an object in my project.

This is file open method (is Excel file)

public void OpenFile(string strPath)
        {
            try
            {                
                InitPOList(MainTool.GetPOList(MainTool.ReadDataFile(strPath)));
                EnableButtons_2();
                //Tool.SaveSetting("LastFile", ofdFileToOpen.FileName);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);                
            }
        }

Such a constructor in my FormMain would be:

   public frmMain(string filePath)
        {
            InitializeComponent();           
            OpenFileFrom(filePath);   

        }

In my project, there is a class ExcelProcess.cs used to process data on the Excel file, I declare the variable:

public static frmMain frmmain;

and processing methods, test data on the Excel file will use this variable for the purpose of loading data from Excel file into the program.

Besides choosing the path directly from Excel file into the program, I build more features: register my program in Window Context Menu, users can choose my program when right into Excel file, and data will also loaded into the program, the work is like choosing the path to the Excel file from the program, in order more easily to the user.

Such methods have to subscribe to Windows Context Menu. Details at: Link Anchor Text

And finally, take the path at voidmain function () in Program.cs

static void Main(string[] args)
{
     frmMain main = new frmMain();

            ExcelProcess.frmmain = main;

            if (args != null && args.Length > 0)
            {
                    Application.Run(new frmMain(args[0])); //Nhận lấy đường dẫn truyền vào chương trình
            }
            else
            {Application.Run(main);}// Phương thức khởi tạo không đối số
}

The problem occurs when I try to get the transmission path to the program, so I have to declare variables to assign the main variable in class ExcelProcess frmmain. And I do not know how to set the args [0] for the main variables that have been assigned to take my program links. When I try to ignore the set args [0] for the main variables by doing the above: Application.Run (new frmMain (args [0])), the program is still running; right click to select the program on Excel occur an error: object reference not set to an instance of an object;
(although I try block's catch catch this error, but the program still get the path to the Excel file and loaded into the program). I think this happens because I have yet to assign the path args [0] for the main variables of the program, but I still have not found a fix.

Hope you guide me fix this error.

The problem I'm having is that I do not know how to main variable:

            frmMain main = new frmMain();
            MainTool.frmmain = main;    

take args[0] in voidmain() function

Therefore, frmmain be declared in MainTool class did not receive value ​​passed. Should an error: object reference not set to an instance of an object. (runtime error) when running the program.

Looking forward to receiving your guidance

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.