NullReferenceException on reading a line during debugging or normal operation

When I tried to read a file when I called DoSomething();(from Something();) in a method, TestProgram_Load()I came across NullReferenceExceptionwith which I am having problems.

This happens when you try to check for a file or even try to read it. However, I can write to the file without problems and refer to the string value even in the debugger.

Here is the problem code:

// No matter the file name, this fails every time.
string fileName = "file.txt";

public void DoSomething()
{
    if (File.Exists(fileName)) // NullReferenceException
    {
        using (StreamReader r = new StreamReader(fileName)) // NullReferenceException
        {

        }
    }
}

And the method that calls it:

public void Something()
{
    // This works fine
    if (!File.Exists(fileName))
    {
        // This works fine
        using (StreamWriter w = new StreamWriter(fileName))
        {
             w.Write("Test");
        }
    }

   // Test to see if there an issue with this method too...
   // This is fine, but whether or not File.Exists(fileName) is used, DoSomething(); has the same problem.

   if (File.Exists(fileName))
   { 
       DoSomething(); 
   }

}

Here's the TestProgram_Load method:

private void TestProgram_Load(object sender, EventArgs e)
{
    TestClass t = new TestClass();
    t.Something();
}

Here's the stack trace:

at TestProgram.TestClass.DoSomething() in Visual Studio 2015\Projects\Test Program\Test Program\Classes\FileSystem\TestClass.cs:line 39
at TestProgram.Program.Main() in Visual Studio 2015\Projects\Test Program\Test Program\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

And line 39:

if (File.Exists(fileName))

This code is executed in the main functions of the program launch: either in the constructor or in the method TestProgram_Load(): both have the same problem. There should be no threads.

Here are some key details:

  • I am trying to read a file, DoSomething();
  • File.Exists(fileName)
  • File.Exists(fileName) , .
  • .
  • .
  • , .

, , , . ?

+4
3

, , , , Stacktrace . List<String> .

, . , , fileName (?) , . .

:

if (File.Exists(fileName)
{
      // [...] many lines down. Do lots of stuff.

      // Culprit
      Class.ListStringObject.Add("string");
}

, , , .

0

, File.Exists :

Exists false, - , . , , .

, , , , , , false. .Net.

, :

  • Thread.Sleep 1000
  • FileInfo (
  • File.Exists , .

, , , FileInfo. - :

var file = new FileInfo(fileName);
if (!file.Exists())
{
    using (var writer = file.CreateText())
    {
        writer.Write("test");
    }
}
using (var writer = file.OpenText())
{
    // do stuff
}

, . .

+2

Make sure you have

using System.IO;

What is a runtime context? Is this IIS host or Windows runtime?

In a web hosting environment, you'll want to do something like this:

var physicalFolder = HostingEnvironment.MapPath(VirtualFolder);
if (File.Exists(physicalFolder + fileName)){
    ...
}

In the context of running Windows, you will want:

var rootOfCurrentPath = Path.GetPathRoot(Environment.CurrentDirectory);
if (File.Exists(rootOfCurrentPath + fileName)){
    ...
}
-1
source

Source: https://habr.com/ru/post/1648545/


All Articles