0

I have written a C# application in Visual Studio (using Avalonia, irrelevant for this question).

It runs perfectly in Visual Studio, yet when I try and run the executable, it throws a NullReferenceException. I have added error checking to find out the error is based on the reading of my settings.json file.

For now, I have placed the settings file in "Documents/ContactFinder/Settings.json"

Essentially when the application starts, it runs my initLoader method which contains this code:

public void initLoader()
{
    string docs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    string path = Path.Combine(docs, "ContactFinder");
    if (!Directory.Exists(path))
    {
        Directory.CreateDirectory(path);
    }

    string settingsPath = Path.Combine(path, "settings.json");
    if (!File.Exists(settingsPath))
    {
        Settings set = new Settings();
        set.restoreDefaults();
        File.WriteAllText(settingsPath, JsonConvert.SerializeObject(set));
    }
}

Then it calls LoadSettings, which then also pushes the settings to the application as follows:

public void LoadSettings()
{
    try
    {
        string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/ContactFinder/settings.json";
        if (!File.Exists(path))
        {
            Console.WriteLine("Settings file not found: " + path);
            throw new FileNotFoundException("Settings file not found", path);
        }

        string json = File.ReadAllText(path);
        Console.WriteLine("Settings file content: " + json);

        Settings set = JsonConvert.DeserializeObject<Settings>(json);
        pushSettings(set);
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error loading settings: {ex.Message}");
        throw;
    }
}

public void pushSettings(Settings set)
{
    if (set == null)
    {
        throw new ArgumentNullException(nameof(set), "Settings object is null");
    }
    ShortPMin = set.pauses.ShortPause.min.ToString();
    ShortPMax = set.pauses.ShortPause.max.ToString();
    MedPMin = set.pauses.MediumPause.min.ToString();
    MedPMax = set.pauses.MediumPause.max.ToString();
    MtlPMin = set.pauses.MtlPause.min.ToString();
    MtlPMax = set.pauses.MtlPause.max.ToString();
    LongPMin = set.pauses.LongPause.min.ToString();
    LongPMax = set.pauses.LongPause.max.ToString();
    PagePMin = set.pauses.PageLoad.min.ToString();
    PagePMax = set.pauses.PageLoad.max.ToString();
    SbEnabled = set.browser?.showBrowser ?? false;
    Users.Clear();
    if (set.logins.users != null)
    {
        foreach (var user in set.logins.users)
        {
            Users.Add(new LoginUsers(user.username, user.password));
        }
    }
    DefaultPages = set.search.defaultPageCount;
    bPages = DefaultPages;
    SelectedUser = Users.FirstOrDefault();
}

Each of the variables set in the PushSettings method are bindings to the View, and all the previous methods listed are in the MainViewModel.cs

When I start debugging in Visual Studio, it runs perfectly; it outputs the contents of settings file to console, and pushes the settings to the app and it starts fine.

Here is how I attempt to start the application on Mac, for reference I am on Arm64. The error is shown here also

thomas@thomass-Mac-mini publish % chmod +x testMacArm642
thomas@thomass-Mac-mini publish % sudo ./testMacArm642
Password:
Settings file content: 
Error loading settings: Settings object is null (Parameter 'set')
Unhandled exception. System.ArgumentNullException: Settings object is null (Parameter 'set')
   at testMacArm642.ViewModels.MainWindowViewModel.pushSettings(Settings set) in /Users/thomas/Projects/testMacArm642/testMacArm642/ViewModels/MainWindowViewModel.cs:line 462
   at testMacArm642.ViewModels.MainWindowViewModel.LoadSettings() in /Users/thomas/Projects/testMacArm642/testMacArm642/ViewModels/MainWindowViewModel.cs:line 503
   at testMacArm642.ViewModels.MainWindowViewModel..ctor() in /Users/thomas/Projects/testMacArm642/testMacArm642/ViewModels/MainWindowViewModel.cs:line 407
   at testMacArm642.App.OnFrameworkInitializationCompleted() in /Users/thomas/Projects/testMacArm642/testMacArm642/App.axaml.cs:line 20
   at Avalonia.AppBuilder.SetupUnsafe()
   at Avalonia.AppBuilder.Setup()
   at Avalonia.AppBuilder.SetupWithLifetime(IApplicationLifetime lifetime)
   at Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime(AppBuilder builder, String[] args, Action`1 lifetimeBuilder)
   at testMacArm642.Program.Main(String[] args) in /Users/thomas/Projects/testMacArm642/testMacArm642/Program.cs:line 13
zsh: abort      sudo ./testMacArm642
thomas@thomass-Mac-mini publish % 

What can I do to allow the application to read the settings file properly and load it into the program?

0

1 Answer 1

0

After doing further testing and printing every variable to console, it turns out that Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) does not return the documents folder path, but the parent folder.

So to solve this issue i had to change:

string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/ContactFinder/settings.json";

into

string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/Documents/ContactFinder/settings.json";
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.