Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
空参考错误C#多个对象_C#_Oop_Variables_Nullreferenceexception - Fatal编程技术网

空参考错误C#多个对象

空参考错误C#多个对象,c#,oop,variables,nullreferenceexception,C#,Oop,Variables,Nullreferenceexception,我得到了以下构造函数: private PicklistHelper(string docId, string prodId, string user, string formProdId, string filepath,string parts) { this.documentId = docId; this.prodId = prodId; this.user = user; this.filepath = filep

我得到了以下构造函数:

private PicklistHelper(string docId, string prodId, string user, string formProdId, string filepath,string parts)
    {
        this.documentId = docId;
        this.prodId = prodId;
        this.user = user;
        this.filepath = filepath;
        this.parts = parts;
        this.formProdId = formProdId;
        Log.Message("test parts = " + this.parts);
    }
logmessage确实显示parts变量的值。 然后我调用一个函数

private ArrayList GetPartDatasets()
    {
        Log.Message("test line 1");
        ArrayList picklist = new ArrayList();
        Log.Message("test line 2 " + parts);
        string[] partIds = parts.Split(new[] { ';' });
        Log.Message("test line 3");
第二个测试行出现在日志中,但parts变量为空。在此之后,将出现一个null ref错误

PicklistHelper对象是在一行中多次调用的函数中创建的。
知道问题的原因吗?

问题是
部分
为空

基本上,我可以从您提供的注释代码中找出两个可能的原因:

  • 如果您所说的是真的,并且在应用程序流的任何可能情况下,至少会调用一次确切的
    PickListHelper
    构造函数,那么仍然有可能调用的
    PickListHelper
    正在为
    部分
    参数传递
    null
    。这可以在构造函数内部通过以下方式进行检查:
    if(parts==null)抛出新ArgumentNullException(“parts”),因为您需要零件参数。否则,您应该找出谁/什么在调用
    PicklistHelper
  • 在初始化
    parts
    字段之前或将其重置为
    null
    之后,调用方法
    GetPartDatasets

  • 空字符串是一种p.I.T.a.在我们的软件中,我们不一致地处理字符串,以至于我们调用空字符串上的方法

    试试这个

    private PicklistHelper(string docId, string prodId, string user, string formProdId, string    filepath,string parts)
        {
            this.documentId = docId??    string.Empty;
            this.prodId     = prodId??   string.Empty;
            this.user       = user??     string.Empty;
            this.filepath   = filepath?? string.Empty;
            this.parts      = parts??    string.Empty;
            this.formProdId = formProdId?? string.Empty;
    
            Log.Message("test parts = " + this.parts);
        }
    
    弦部件

     if (parts != null)
            {
                string[] partIds = parts.Split(new[] { ';' });
                Log.Message("test line 3" + partIds);
            }
    
    听起来不错

    问候,,
    Sekhar

    PicklistHelper
    类中的
    GetPartDatasets()
    方法吗?您是否使用多个线程(直接或间接通过计时器流逝事件)可能存在重复?如何使用私有构造函数实例化
    PicklistHelper
    对象?
    parts
    GetPartDatasets
    中为null,它在日志中显示空消息,并在下一行抛出
    null
    错误这是否掩盖了错误时传递的null参数?也许使用不同的构造函数重载甚至可选参数可以更好地设计它。重点是防止空引用异常。可能需要进行更多的参数检查。但是如果应该存在NullReferenceException,则NullReferenceException比完全没有错误要好得多。这就是为什么在执行方法内部的操作之前,应该检查方法的参数是否存在无效值的原因。在尝试对变量进行操作之前,请先确定要使用的值为null。恶意null引用异常并不更好。现在,如果
    PicklistHelper
    捕捉到并抛出了它,那么OK。然而,更好的是健壮的代码,它可以处理“空”属性,而不会引起任何挑衅。