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;
}
}