即使在事先检查下一个innertext值之后,XMLNodeList也会引用null引用

时间:2014-11-11 08:56:13

标签: c# .net arrays xml list

我试图从下一个项目中获取值,例如,如果下一个项目为null,或者不存在,它将继续打印。我所面临的主要问题仅在于这一部分

主要问题:

try
{
 numCheck = productList[j+1]["Number"].InnerText;
}
catch (Exception ex)
{
    numCheck = string.Empty;
}

if (numCheck != null)
{
     try
     {                                
          mainNumber = Data.getFullItemDetail(productList[j + 1]["Number"].InnerText);                                
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.ToString());
     }

我在尝试之前做过检查,否则只是为了确保该值存在,但是,即使它检测到它有值,该行也会抛出错误NullReference

mainNumber = Data.getFullItemDetail(productList[j + 1]["Number"].InnerText);

完整代码以防万一:

XmlDocument xml = new XmlDocument();
xml.LoadXml(productItems);
XmlNodeList productList = xml.SelectNodes("/Products/Product"); 

for (int j = 0; j < productList.Count; j++) 
{

     itemCount++;
     int i = j + 1;

     string item = productList[j]["Number"].InnerText;         

     number = Data.getFullItemDetail(item);

            try
            {
                #region mainItem
                if (number == 0)
                {

                    itemCount++;
                    itemName = productList[j]["Name"].InnerText;

                    try
                    {
                        numCheck = productList[i]["Number"].InnerText;
                    }
                    catch (Exception ex)
                    {
                        numCheck = string.Empty;
                    }

                    if (numCheck != null)
                    {
                        try
                        {                                
                            mainNumber = Data.getFullItemDetail(productList[i]["Number"].InnerText);                                
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString());
                        }

                        if (mainNumber == 0)
                        {                                
                            foreach (string condimentItem in condiment)
                            {
                                condimentText += condimentItem.ToString() + "\n";
                            }

                            string text = condimentText + itemName + "\n" + employeeNum + "\n" + terminalNum + "\n";

                            //print here, because the next item is another main item.


                            };

                            try
                            {
                                p.Print();
                            }
                            catch (Exception ex)
                            {
                                throw new Exception("Exception Occured While Printing", ex);
                            }
                        }
                        else
                        {
                            MessageBox.Show("Error");
                        }

                    }
                    else
                    {                            
                        foreach (string condimentItem in condiment)
                        {
                            condimentText += condimentItem.ToString() + "\n";
                        }

                        string text = condimentText + itemName + "\n" + employeeNum + "\n" + terminalNum + "\n";                            
                    }
                }
                #endregion

                #region CondimentItem
                else if (number == 4)
                {                        
                    condiment.Add(productList[j]["Name"].InnerText);

                    try
                    {
                        numCheck = productList[i]["Number"].InnerText;
                    }
                    catch (Exception ex)
                    {
                        numCheck = string.Empty;
                    }

                    //Check next item.                    
                    if (numCheck == null)
                    {                            
                        foreach (string condimentItem in condiment)
                        {
                            condimentText += condimentItem.ToString() + "\n";
                        }

                        string text = condimentText + itemName + "\n" + employeeNum + "\n" + terminalNum + "\n";

                        //print here, because the next item is another main item.

                    }
                    else
                    {

                        mainNumber = Data.getFullItemDetail(productList[i]["Number"].InnerText);

                        if (mainNumber == 0)
                        {
                            foreach (string condimentItem in condiment)
                            {                                    
                                condimentText += condimentItem.ToString() + "\n";
                            }

                            string text = condimentText + itemName + "\n" + employeeNum + "\n" + terminalNum + "\n";

                            //print here, because the next item is another main item.

                        }
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

已编辑:Data.getFullItemDetail仅返回1个int值。

    public static int getFullItemDetail(string number)
    {
        int num = Convert.ToInt32(number);
        int numReturn;

        using (SqlConnection conn = getConnectionSAP())
        {
            SqlCommand comm = new SqlCommand("select belongcond from ------ where number = " + num, conn); 

            conn.Open();
            SqlDataReader dr = comm.ExecuteReader();
            while (dr.Read())
            {
                num = Convert.ToInt32(dr["-------"]);
            }
            comm.Clone();
            comm.Dispose();
        }

        num = Convert.ToInt32(num.ToString().Substring(0, 1));

        return num;
    }

1 个答案:

答案 0 :(得分:0)

productList[j+1]["Number"]为空,因此在第二行引发异常(因为您无法调用null.InnerText),捕获然后设置numCheck = string.Empty

您为if (numCheck != null)测试的下一行, true,因为您只是在异常处理程序中将其设置为string.Empty 。然后,您再次在InnerText上致电productList[j+1]["Number"],再次抛出NullReferenceException

设置断点,遍历代码,检查变量,正如What is a NullReferenceException and how do I fix it?中所述。

只需将if (numCheck != null)更改为if (productList[j+1]["Number"] != null)