0

I keep getting an error:

System.NullReferenceException: Object reference not set to an instance of an object.

Everytime I run the application outside the IDE, but for some magical reason, it works fine inside the IDE. I am definitely sure the error is caused by this code as the app ran smoothly when I removed it:

 Public Function GetCommonFolder() As String
        On Error GoTo ErrH
        Dim winPath As String = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
        Dim commonfolderpath As String
        commonfolderpath = Replace(winPath & "\MyApp Data", "\\", "\")
        If My.Computer.FileSystem.DirectoryExists(commonfolderpath) = False Then
            System.IO.Directory.CreateDirectory(commonfolderpath)
        End If
        GetCommonFolder = commonfolderpath
        Exit Function
ErrH:
        GetCommonFolder = ""
        Msgbox("Error retrieving common folder")
    End Function

Does anyone here know what is causing this annoying problem?

2
  • 2
    which line no. gives the error ? Commented Jul 4, 2013 at 10:11
  • When I changed this line: Dim winPath As String = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) to some dummy value, everything worked fine for some reason. Commented Jul 4, 2013 at 10:15

1 Answer 1

1

It seems like the user that you run the program on outside the IDE doesn't have access to the common application data folder. Try executing it by "Run as administrator". Are you running on Windows Vista or newer? Maybe you have to require UAC elevation?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.