0

I am completely stumped on this one, I have a static class attempting to detect if a directory exists, but for some reason, it throws the following error:

    Program.Main encountered an error: Object reference not set to an instance of an object. Stack trace:    at csv.prepareCSVData() in path/csv.cs:line 21
   at RLCSVTools.Program.Main(String[] args) in path\Program.cs:line 31

This is the code that produces that error in csv.cs.prepareCSVData:

ConfigurationSync.logDebugMessage(logMessageType.warning, "CSV class Dir: " + exportPath);
//this log works and reveals exportPath has been populated
if (Directory.Exists(exportPath) == false)
    //breaks here regardless of dir existing or not
{
    ConfigurationSync.logDebugMessage(logMessageType.warning, "Recreating the directory: " + exportPath);
    // I have never seen this log run
    Directory.CreateDirectory(exportPath);
}

I have added some comments in the code to show at exactly what line the error occurs.

All members of this class, including the class, are static. public static class csv

Has anyone experienced anything like this? I can't seem to find a solution.

Tiaan
  • 698
  • 2
  • 10
  • 32
  • In this exact example, what is the value of exportPath? – zakharuk_pasha Nov 02 '18 at 12:17
  • Any chance you could provide a [mcve]? It's quite hard to tell exactly what's going on here with quite a lot of context missing. – Jon Skeet Nov 02 '18 at 12:18
  • This is what's stored in exportPath: "C:\RL - Emperio\purchase-export\" – Tiaan Nov 02 '18 at 12:22
  • 1
    I don't think it's `Directory.Exists()` that's throwing the exception. – Matthew Watson Nov 02 '18 at 12:23
  • One of the main reasons we close this kind of question as a duplicate of [What is a NullReferenceException and how do I fix it](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) is because there is a bit of debugging that needs to happen before this question is posted. Can you please go through the debugging steps and figure out exactly *what* is null here, or at least remove all the code that doesn't impact the problem? – Lasse V. Karlsen Nov 02 '18 at 12:29
  • @LasseVågsætherKarlsen "place strategic breakpoints and inspect your variables": I have done this, and the program exits after running Directory.Exists(exportPath), before breaking hovering over the code shows that the variable is populated correctly... There is only one place where this function is called and all variables are private and never assigned to in the program other than initialization which happens as a private global variable. path = @"C:\RL - Emperio\purchase-export\" – Tiaan Nov 02 '18 at 12:35
  • Is `exportPath` a property, field or local variable? – Lasse V. Karlsen Nov 02 '18 at 12:42
  • Are you sure, that you are using `System.IO.Directory` and that your `using` is correct? Please check if `Directory` is NOT NULL. – nilsK Nov 02 '18 at 13:27

1 Answers1

0

So lets look at the documentation

Directory.Exists(String) Method

Doesn't throw any exception

CreateDirectory(String)

Creates all directories and subdirectories in the specified path unless they already exist.

Exceptions

  • IOException
    • The directory specified by path is a file. -or-
    • The network name is not known.
  • UnauthorizedAccessException
    • The caller does not have the required permission.
  • ArgumentException
    • path is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the GetInvalidPathChars() method. -or-
    • path is prefixed with, or contains, only a colon character (:).
  • ArgumentNullException
    • path is null.
  • PathTooLongException
    • The specified path, file name, or both exceed the system-defined maximum length.
  • DirectoryNotFoundException
    • The specified path is invalid (for example, it is on an unmapped drive).
  • NotSupportedException
    • path contains a colon character (:) that is not part of a drive label ("C:\").

Its clear that CreateDirectory(String) is not your problem

So by deducation the only obvious issue here is exportPath is null

For which this is relevant

What is a NullReferenceException, and how do I fix it?

if exportPath is not null, then you need to debug your application, something is not what it seems

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • But I have determined the variable is not empty/null? The log returns the exportPath I commented below my question – Tiaan Nov 02 '18 at 12:27
  • You won't get NullReferenceException from CreateDirectory either, even if `exportPath` is null, you will get `ArgumentNullException`, so the problem here is not in these calls. – Lasse V. Karlsen Nov 02 '18 at 12:27