Click here to Skip to main content
15,912,312 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i tried to get files using OpenFileDialog and store it to list.

private void upload()
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Title = strings.BROWSE_YOUR_DOCUMENTS;
            openFileDialog1.Filter = "PDF Files|*.pdf|Office Files|*.doc;*.docx;*.xls;*.xlsx;*.ppt;*.pptx|txt files (*.txt)|*.txt|Image Files(*.BMP;*.JPG;*.JPEG;*.PNG;*.GIF)|*.BMP;*.JPG;*.JPEG;*.PNG;*.GIF|All files (*.*)|*.*";
            openFileDialog1.CheckFileExists = true;
            openFileDialog1.CheckPathExists = true;
            openFileDialog1.Multiselect = true;
            openFileDialog1.FilterIndex = 5;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                
                foreach (String file in openFileDialog1.FileNames)
                {
                    try
                    {
                        string name = Path.GetFileName(file);
                        string path = file;

                        Image img = file.GetThumbnail();
                        CLSFiles addfile = new CLSFiles();
                        addfile.id = 1;
                        addfile.name = name;
                        addfile.docNumber = "0";
                        addfile.operation_Name = opName;
                        addfile.operation_ID = opid;
                        addfile.Path = path;
                        addfile.thumb_Blob = img;
                        addfile.Status = 1;
                        files.Add(addfile);
                    }catch (Exception ex)
                    {
                        Msg.Show("", ex.ToString(), 0);
                    }
                   
                }
            }
            cLSFilesBindingSource.DataSource = files.ToList();
            DocViews.FocusedRowHandle = DocViews.DataRowCount - 1;
            loadRecordData();
        } 


Update 1 :
this the full code of CLSFiles

public List<CLSFiles> files = new List<CLSFiles>();



public class CLSFiles
   {

       private int _id;
       private string _name;
       private string _docNumber;
       private string _operation_Name;
       private int? _operation_ID;
       private Nullable<sbyte> _Deafult;
       private string _Deafult_String;
       private Nullable<sbyte> _Status;
       private string _Status_String;
       private Image _thumb_Blob;
       private string _Path;



       public int id
       {
           get { return this._id; }
           set { this._id = value; }
       }
       public string name
       {
           get { return this._name; }
           set { this._name = value; }
       }
       public string docNumber
       {
           get { return this._docNumber; }
           set { this._docNumber = value; }
       }
       public string operation_Name
       {
           get { return this._operation_Name; }
           set { this._operation_Name = value; }
       }
       public int? operation_ID
       {
           get { return this._operation_ID; }
           set { this._operation_ID = value; }
       }
       public sbyte? Deafult
       {
           get { return this._Deafult; }
           set { this._Deafult = value; }
       }
       public string Deafult_String
       {
           get { return this._Deafult_String; }
           set { this._Deafult_String = value; }
       }
       public sbyte? Status
       {
           get { return this._Status; }
           set { this._Status = value; }
       }
       public string Status_String
       {
           get { return this._Status_String; }
           set { this._Status_String = value; }
       }
       public Image thumb_Blob
       {
           get { return this._thumb_Blob; }
           set { this._thumb_Blob = value; }
       }
       public string Path
       {
           get { return this._Path; }
           set { this._Path = value; }
       }


   }


What I have tried:

I wrote the attached code

but this error appear :

System.NullReferenceException: 'Object reference not set to an instance of an object.'
files was null at this line files.Add(addfile);
Posted
Updated 30-Nov-20 23:55pm
v3
Comments
[no name] 30-Nov-20 12:15pm    
Don't see any "files" object.
Golden Basim 30-Nov-20 13:23pm    
yes it declared, please check the updates.
The Other John Ingram 30-Nov-20 12:23pm    
you are missing the List<clsfiles> or is it declared somewhere else
Golden Basim 30-Nov-20 13:23pm    
yes it declared, please check the updates.
The Other John Ingram 30-Nov-20 18:25pm    
ok, its declared, but unless its global to the entire program how is the upload routine supposed to know about it?

1 solution

We can't see what else you do with the files variable, but it's likely that you haven't created any collection to put into it.

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterday's shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
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