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.