Click here to Skip to main content
15,895,491 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,

I should First thank you all for taking time reading and kindly answering my question. It was suggested that I should set the Array Size at the time of definition.

I understand it but the fact is, at the time of definition, I am not aware of its probable size needed. As it was pointed I have to define the ‘FileNames’
which is an Array of string outside of button1_Click event (due to some reasons). But within this event the size of it will become clear. So I don’t know it in advance.

In other words, what should I do when I need to define an array with unknown size at the time of definition so that it won’t raise such error?

In bellow my question is repeated for convenience

*************

Hello,

In a program the user may choose some files to have some operation on. In ‘OpenFileDialog’, Full path of files, both path of directory and File names could be returned. What I require is to separate File path from its following File name. To do so, I needed to set value to Array of string one by one. I used the code below but there is a error as follows:

Null Reference Exception was unhandled
Object reference not set to an instance of an object.

Could you please help me?
Thanks a lot


public Form1()
        {
            InitializeComponent();
        }
 
 string[] FilePathNames;
        string[] FileNames; // I need to define here, instead of defining inside 'button1_Click' method 

        private void button1_Click(object sender, EventArgs e)
        {
 
           ... 
 

            if (myOpenFileDialog.ShowDialog() == DialogResult.OK)
            {
                 FilePathNames = myOpenFileDialog.FileNames;
                //FilePathNames, includes Full path of files, both path of                                directory and File names
                                            
                int counter = 0;
 
                // in loop below, i want to detach File path from its following File name
                foreach (string fname in FilePathNames)  
                {
                    int idx = fname.LastIndexOf('\\');
                    FileNames[counter] = fname.Substring(idx + 1); // Here, Error Ocuurs!!!
                    counter += 1;
                }
            }
 
        }
Posted

Re-initializing the array once you know how many files you are getting from the OpenFileDialog is a good idea... beyond that, you may want to look into using a List. You do not need to know how many objects you are going to add to use them.

(if there is a couple tags at the end of this... I don't know why they're there after I post, ignore them.)

List<string> fileList = new List<string>();

// in loop below, i want to detach File path from its following File name
                foreach (string fname in FilePathNames)
                {
                    int idx = fname.LastIndexOf('\\');
                    fileList.Add(fname.Substring(idx+1));
                }
</string></string>
 
Share this answer
 
v2
Comments
Vivek Krishnamurthy 29-Jun-11 14:14pm    
My 5!
Morl99 29-Jun-11 14:16pm    
FYI, you can avoid the Tags if you write < instead of < and > instead of >.
ahhashemi 29-Jun-11 14:57pm    
I think it’s better to replace
List fileList = new List ();
With
ArrayList fileList = new ArrayList ();
Add this just before the foreach
string[] tempFileNames = new string[myOpenFileDialog.FileNames.Length];

and copy to ur variable ( you can shallowcopy,deep copy)
FileNames = tempFileNames; // just template
 
Share this answer
 
v3
if openFileDialog return a null value this error occrod

try error handling in your code

and second error (Object reference not set to an instance of an object):
define variable this type
C#
string[] FileNames=new string[300];


hope this help
 
Share this answer
 

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