如何在使用两个单独的类时解决NullReferenceException?

时间:2013-12-21 22:54:06

标签: c# class dictionary

当我运行代码时,它给了我这个错误:

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.

我如何解决这个问题?

 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."},                            
            };
        }
    }
}

5 个答案:

答案 0 :(得分:0)

根据你的代码:

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

很明显ExManager.ExceptionLibrarynull,因为它在setter中被错误地实例化,可能永远不会被使用。您可以在构造函数中设置ExceptionLibrary,然后不再需要setter:

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; }
    }

注意:未使用value的setter很可能被错误使用,应该重新考虑。

答案 1 :(得分:0)

这段代码

set
{
    exceptionlibrary = new Dictionary<string, string>()
        {
            //User
            {"10000", "Invalid user input."},
            {"10001", "Phone number is already registered."},
        }; 
}
任何人都不会调用

,因此ExceptionLibrary的引用为null

您应该在引用此属性之前先调用setter,或者在构造函数中进行初始化。

答案 2 :(得分:0)

永远不会调用“set”,因此当你“获取”它时,exceptionLibrary为null。

也许这是一种更好的方式:

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; }
    }
}

答案 3 :(得分:0)

只有在为属性

指定值时才会激活属性设置器
ExManager.ExceptionLibrary = null; // For example

因为你不这样做,你的字典永远不会被初始化。像这样初始化它并删除setter,因为你很可能不需要为它分配另一个字典。

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; }
    }
}

相反,您也可以使用延迟初始化

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;
    }
}

或者您可以在类'构造函数中初始化字典。

答案 4 :(得分:0)

正确的解决方案是通过在发生异常的行上设置断点,然后重新调试应用程序/代码来使用调试器。

当断点被击中时,你进入&#34;代码,逐行,并检查变量。您可以通过将鼠标光标悬停在视觉工作室中进行检查,或将它们拖动到监视窗口中。您也可以右键单击并添加观察&#34;。

这种策略应该有助于解决任何异常类型,特别是当代码中的几层深层原因,或者它是由同一行代码中的几个表达式之一引起时。

希望有所帮助!