Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi,

I am using the following the following code for saving path of folder browse into settings

C#
private void btnDatabaseGwMsgsBrowse_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.GatewayDataPath = AttachFiles(txtDatabaseGwMsgsFile);
            
        }

        private string AttachFiles(Control control)
        {
            OpenFiles.Filter = Constants.FILTER_GW_DATABASE;
            OpenFiles.Title = Constants.FILTER_GW_DATABASE;

            DialogResult result = this.OpenFiles.ShowDialog();

            if (result == DialogResult.OK)
            {
                control.Text = string.Empty;
                control.Text = OpenFiles.SafeFileName.Trim();
            }
            return (OpenFiles.FileName.Replace(OpenFiles.SafeFileName, " ").Trim());
            //if (result == DialogResult.OK)
            //    DgView.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value = OpenFiles.SafeFileName.Trim();
        }


I have given like below in designer
C#
private System.Windows.Forms.OpenFileDialog OpenFiles;



warning:
Field 'SCANLA.GatewayMsgIDs.OpenFiles' is never assigned to, and will always have its default value null

when I am trying to run the program

Exception:
C#
An unhandled exception of type 'System.NullReferenceException' occurred 
Posted
Comments
EduChapow 7-Feb-14 8:15am    
which row this exception is throwing?

The error messages are telling you all you need to know;
Field 'SCANLA.GatewayMsgIDs.OpenFiles' is never assigned to, and will always have its default value null	


Means that you're never assigning the variable OpenFiles, so it will always be null and you will get a NullReferenceException thrown at you as soon as you try to use one of its members.

Make sure you assign the OpenFiles variable before first use and you'll be fine.

Hope this helps,
Fredrik
 
Share this answer
 
The warning is the key.

the OpenFiles object is never assign, and when you use it, you get the NullReferenceException.

C#
private System.Windows.Forms.OpenFileDialog OpenFiles = new System.Windows.Forms.OpenFileDialog();


should correct your problem.
 
Share this answer
 
Comments
Member 10408451 7-Feb-14 13:14pm    
Hi,
I tried like below but the open file dialog box is opening again and again when I press ok

private System.Windows.Forms.OpenFileDialog OpenFiles = new System.Windows.Forms.OpenFileDialog();
Pascal-78 7-Feb-14 14:53pm    
The OpenFileDialog window is displayed only when OpenFiles.ShowDialog() is called. If you do not have this line elsewhere in your code, that means your AttachFiles method is called.
You should debug your code, put a breakpoint on the ShowDialog line and then look at the call-stack to understand why it's repeating this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900