-1

EDIT: I forgot to add the exception

I've made this code , trying to read multiple files into one string only (later i can split'em, each file has a words in its end like a delimiter).

But everytime i try to open files, it throws me an exception: Additional information: Object reference not set to an instance of an object.

I tried changing the code but didn't worked. I'm new to C# and couldn't find what i'm doing wrong. Any help will be appreciated. PS: I'm using a separated class to hold my variables - since i know i'm gonna need some of 'em in other parts of the code i decided to make them global.

Thanks

The code:

private void openPPFToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog open = new OpenFileDialog())
            {
                // Filter for PPF
                open.Filter = "PPF Files|*.PPF";
                open.Multiselect = true;
                open.Title = "Select a PPF File";
                if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
//Obtaining list of filenames
                    vars.fullFileName = new List<String>(open.FileNames);
                    vars.filepath = open.FileName;
                    foreach (string fileName in vars.fullFileName)
                    {
                        LoadedFiles.Items.Add(fileName.Substring(fileName.LastIndexOf(@"\") + 1));  
                    }
                    for(int i=0; i< vars.fullFileName.Count; i++)
                    {
                        using (var sr = new StreamReader(vars.filepath))
                        {
                            vars.files[i] = sr.ReadToEnd(); //I supposed that each string position could hold an entire file.
                        }
                        string teste1 = vars.files[3].ToString(); //Just trying to show the contents
                        textBox1.Text = teste1;
                    }


                }
            }
        }

The Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PPF_Converter_2._0
{
    class vars
    {
        public static List<String> fullFileName;
        public static string filepath;
        public static List<String> textdata;
        public static string sLine = "";
        public static string data;
        public static string[] files;


    }
}
Pablo Costa
  • 115
  • 2
  • 14

2 Answers2

0

Your array "files" isn't initialized. You need something like this:

Files = new string[3];

If the array should hold 3 elements.

Marco Rebsamen
  • 511
  • 6
  • 22
0

I think you have problem with file reading.

Try like this.

  foreach (String file in openFileDialog1.FileNames) 
    {
      string fileContent = File.ReadAllText(file);
     //do your activity here
    }
Keppy
  • 367
  • 1
  • 5
  • 16
  • Combning your answer with marco's one (about initializing the array) i got what i wanted. Created an string[] array wich each position holding an entire file. Since i also have the filenames in other variable (in the same order) i can begin working on my stuff. Thanks a lot ! – Pablo Costa Feb 17 '16 at 19:13