简体   繁体   中英

How to resolve NullReferenceException when using two separate classes?

When I run the code it gives me this error:

An exception of type 'System.NullReferenceException' occurred in App.dll but was not handled in user code

Additional information: Object reference not set to an instance of an object.

How would I resolve this?

 ExceptionHandling ExManager = new ExceptionHandling();
 var Message = ExManager.ExceptionLibrary["10000"]// occured here

class ExceptionHandling

public class ExceptionHandling
{
    private Dictionary<string, string> exceptionlibrary;
    public Dictionary<string, string> ExceptionLibrary
    {
        get { return exceptionlibrary; }
        set
        {
            exceptionlibrary = new Dictionary<string, string>()
            {
                //User
                {"10000", "Invalid user input."},
                {"10001", "Phone number is already registered."},                            
            };
        }
    }
}

According to your code:

var Message = ExManager.ExceptionLibrary[10000]// occured here

it is clear that ExManager.ExceptionLibrary is null , because it is instantiated wrongly in the setter, that probably is never used . You can set ExceptionLibrary in constructor and then setter is not needed anymore:

public class ExceptionHandling
{
    public ExceptionHandling()
    {
        exceptionlibrary = new Dictionary<string, string>()
        {

            //User
            {"10000", "Invalid user input."},
            {"10001", "Phone number is already registered."},


        }; 
    }

    private Dictionary<string, string> exceptionlibrary;
    public Dictionary<string, string> ExceptionLibrary
    {
        get { return exceptionlibrary; }
    }

Note : setter that is not using value is most probably wrongly used and should be rethinked again.

This piece of code here

set
{
    exceptionlibrary = new Dictionary<string, string>()
        {
            //User
            {"10000", "Invalid user input."},
            {"10001", "Phone number is already registered."},
        }; 
}

is not called by anyone, so the reference of ExceptionLibrary is null .

You should call the setter first before referencing this property, or maybe doing the initialization inside the constructor.

The "set" is never called and therefore exceptionLibrary is null when you "get" it.

Perhaps this is a better way:

public class ExceptionHandling
{
    private Dictionary<string, string> exceptionlibrary = new Dictionary<string, string>       
        {
            //User
            {"10000", "Invalid user input."},
            {"10001", "Phone number is already registered."},
        };

    public Dictionary<string, string> ExceptionLibrary
    {
        get { return exceptionlibrary; }
    }
}

A property setter is only activated when a value is assigned to the property

ExManager.ExceptionLibrary = null; // For example

Because you don't do that your dictionary will never be initialized. Initialize it like this instead and remove the setter, because you will very probably not need to assign another dictionary to it.

public class ExceptionHandling
{
    private Dictionary<string, string> exceptionlibrary = 
        new Dictionary<string, string>() {
            {"10000", "Invalid user input."},
            {"10001", "Phone number is already registered."},
        };

    public Dictionary<string, string> ExceptionLibrary
    {
        get { return exceptionlibrary; }
    }
}

Instead you could also use a lazy initialization

public Dictionary<string, string> ExceptionLibrary
{
    get {
        if (exceptionlibrary == null) {
            exceptionlibrary = new Dictionary<string, string>() {
                {"10000", "Invalid user input."},
                {"10001", "Phone number is already registered."},
            };
        }
        return exceptionlibrary;
    }
}

or you can initialize the dictionary in the class' constructor.

The correct solution is to put your debugger to use by setting a breakpoint on the line the exception occurs, then re-debugging the application/code.

When the breakpoint is hit you "step into" the code, line by line, and inspect variables along the way. You can inspect them in visual studio by hovering the mouse cursor, or dragging them into a Watch Window. You can also right-click and "Add Watch".

This tactic should help with resolving any exception type, especially when the cause my be several layers deep in the code, or when it's being cause by one of several expressions on the same line of code.

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM