2

there. I have a Application that groups methods in a Class, so they are Called when user Pressed the Key that Class has in its field (everything is fine with this), but at the moment I declare a field called "paramsGroup" (a List of objects where I store the Parameters that those options may need), It started giving me an Exception when trying to create a new Instance of any class that has that List initialized.

Screenshot where the Exception ocurrs

As far as I can see, the Exception is thrown at the Constructor of my Class called 'FileManager' at line 19 (where I declare a new instance of the field 'paramsGroup' with no success).

/// <summary> Performs the Functions related to the File Manager of this Program. </summary>

internal class FileManager : Framework
{
/// <summary> Creates a new Instance of the FileManager. </summary>

public FileManager()
{
this.ID = 1;
this.name = Text.LocateByID(Interface.stringsList, "FRAMEWORK_FILE_MANAGER");

this.paramsGroup = new List<object>() // Causes exception in the Constructor
{
// Param #0 - Input Path
Loader.LocateResource<InputPathDialog>(Interface.dialogs).Display(),

// Param #1 - Output Path
Loader.LocateResource<OutputPathDialog>(Interface.dialogs).Display(),

// Param #2 - New Name
Loader.LocateResource<NewNameDialog>(Interface.dialogs).Display()
}; // Code fragment...

The class is supossed to hold the Parameters needed to make calls to the methods of my class Called 'FileManager' by Indexing the List (I also tryed with Array and got the same result). Inside that List you can see some Variables that are Types loaded from a Dictionary that stores objects and cast them to the Instace of the generic type (T) specified in the method 'LocateResource' from the 'Loader' class of the Program.

Note that the objects inside the Dictionary 'Interface.dialogs' are obtained by filtering all the type of the Assembly with a namespace given. After I check the Types belong to the expected namespace, I create a Instace of them with the method 'Activator.CreateInstance' and save it in the Dictionary within the typeName of the object.

The method 'LocateResource' from my class 'Loader'.

/** <summary> Locates a Resource from a Dictionary with the Specific Type. </summary>
<param name = "sourceDictionary" > The Dictionary to be Filtered. </param>

<returns> The Instance of the Resource obtained. </returns> */

public static T LocateResource<T>(Dictionary<string, object> sourceDictionary)
{
Type resourceType = typeof(T);
return (T)sourceDictionary[resourceType.Name];
}

At the other hand, when I need to overload a method with any argument, I just retrieve the parameters from the List, like this:

this.functionsList = new List<Function>()
{
// Function A - Display File Properties

new Function()
{
ID = ConsoleKey.A,
name = Text.LocateByID(Interface.stringsList, "FUNCTION_DISPLAY_FILE_PROPERTIES"),
process = Task_Manager.CreateTask( () => Archive_Manager.DisplayFileProperties(paramsGroup[0] as string) ) // Retrieve first element from 'paramsGroup' and cast it to string
}

}

As I said before, this was working fine yesterday until I declared the field 'paramsGroup' as Part of my class. If you have some tips for avoiding this Exception I would be more than grateful if you share them.

What I tried: to check if the field was giving Exception because it was 'protected', but even public or not, the isssue persists. Also I tryed changing the type of the field from 'List' to 'object[]', with the same results.

What I expect: a way to store params in a Sorted way, so I can retrieve them any time I need to make a method call that requieres some arguments in order to be overloaded. (Note that the parameters may vary according to user choice).

  • 1
    Post the exception as *text* in the question itself – Panagiotis Kanavos Jul 11 '23 at 12:40
  • If you check the text of the exception you'll see it complains about a NullReferenceException thrown by a Loader class. Is this your own class or an external library? I can't copy the relevant line because the exception is an image instead of text – Panagiotis Kanavos Jul 11 '23 at 12:42
  • You code throws null reference exception. Try debugging it. It seems that one of the `Loader.LocateResource<...>(Interface.dialogs)` returns null and you are trying to call `Display()` on it. – Guru Stron Jul 11 '23 at 12:44
  • Thanks, I think why it happens. The other days that method was obtaining Compiler Generated classes (those that end with 'c<>'). As far as I know, that type of classes are only accesible through the compiler, so when attempting to access them, It will throw NullReferenceException (that's what I understand, since this was working fine and the classes are saved as objet initialized). –  Jul 11 '23 at 16:14
  • @JustaCoder compiler-genrerated classes are accessible via reflection (though usually I would recommend against it) – Guru Stron Jul 11 '23 at 17:04

0 Answers0