Ive created this class to handle file io
public class FileStreamManager
{
public FileStream fileStream;
public StreamWriter streamWriter;
public StreamReader streamReader;
public bool CreateOrAppendFileForOutput(string fileName)
{
try
{
fileStream = new FileStream(fileName, FileMode.Append, FileAccess.Write);
streamWriter = new StreamWriter(fileStream);
return true;
}
catch (Exception) { }
return false;
}
public void CloseCreateOrAppendFileForOutput()
{
try
{
fileStream.Dispose();
streamWriter.Dispose();
}
catch (Exception) { }
}
}
and im using it in another class like
FileStreamManager fsmExcpLog = new FileStreamManager();
fsmExcpLog.CreateOrAppendFileForOutput(todaysTestDirPath + @"\ExceptionLog.txt");
FileStreamManager fsmOffersLog = new FileStreamManager();
fsmOffersLog.CreateOrAppendFileForOutput(todaysTestDirPath + @"\OffersLog.txt");
FileStreamManager fsmTestedAddLog = new FileStreamManager();
fsmExcpLog.CreateOrAppendFileForOutput(System.AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["AddressLogFilePath"].Trim());
but when i try to write using this
fsmTestedAddLog.streamWriter.WriteLine("Some Data");
it is giving me Object reference not set to an instance of an object Error. i looked at the abject stream variable while debugging they are null, My question is why are they getting null if i have not called CloseCreateOrAppendFileForOutput() method yet?
I think the references are null
because you set them in a try
block. Maybe try creating a new object, and setting the files they reference within the try
block?