How to fix a C# method that produces a result but returns null to the calling method

Viewed 58

I have this class that, when run in a console app, produces a formatted json string. Now I want to call this method and return the result.

I'm calling it this way var typeRefPath = GetLists.GetTechType(); But it tells me that it is returning null.

What am I doing wrong?

using System.Linq;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace AutosortLockers
{
    class GetLists
    {
        public static string TechList { get; set; }

        public static string GetTechType()
        {
            // Load categories.json
            JObject catObj = JObject.Load(new JsonTextReader(File.OpenText(Mod.GetModPath() + "/categories.json")));
            // Load techtypes.json
            JObject ttObj = JObject.Load(new JsonTextReader(File.OpenText(Mod.GetModPath() + "/techtypes.json")));

            foreach (var categoriesJson in catObj)
            {
                // Filter variables
                var gameVersions = new HashSet<char> { 'A', '2' };
                var categoryIDs = new HashSet<string> { "metals", "tablets" };
                var useInMod = new HashSet<bool> { true };

                // Right outer join on catObj.  Select all Items[*] array items
                var query = from c in catObj.SelectTokens("Categories[*]").OfType<JObject>()
                                            // Join catObj with ttObj on CategoryID
                                        join t in ttObj.SelectTokens("TechTypes[*]") on (string)c["CategoryID"] equals (string)t["CategoryID"]
                                        // Process the filters
                                        where categoryIDs.Count() > 0 ?
                                        useInMod.Contains((bool)c["UseInMod"])
                                        && gameVersions.Contains((char)c["GameVersion"])
                                        && gameVersions.Contains((char)t["GameVersion"])
                                        && categoryIDs.Contains((string)c["CategoryID"]) :
                                        useInMod.Contains((bool)c["UseInMod"])
                                        && gameVersions.Contains((char)c["GameVersion"])
                                        && gameVersions.Contains((char)t["GameVersion"])
                                        select new
                                        {
                                            CategoryDescription = c["CategoryDescription"],
                                            CategoryID = c["CategoryID"],
                                            TechName = t["TechName"],
                                            TechType = t["TechType"],
                                            TechID = t["TechID"],
                                            GameVersion = t["GameVersion"]
                                        };
                // Materialize the query into a list of results.
                 TechList = JsonConvert.SerializeObject(query.ToArray(), Formatting.Indented);
            }
            return TechList;
        }
    }
}```
2 Answers

Use a debugger and check if you ever reach by breakpoint:

TechList = JsonConvert.SerializeObject(query.ToArray(), Formatting.Indented);
        

When you don't reach this line, it will use the default value (which is null) since you declared it as static. If you declare it locally (in your method), your IDE should warn you that declared a variable which was potentially never initialized (https://www.pluralsight.com/guides/declaring-and-initializing-variables-in-c).

I guess your query on the json is wrong, but without the data this is impossible to tell.

Try changing the line from

TechList = JsonConvert.SerializeObject(query.ToArray(), Formatting.Indented);

to

TechList += JsonConvert.SerializeObject(query.ToArray(), Formatting.Indented);

Related