0

I am trying to get the value from the next item, for example, if the next item is null, or doesnt exist, it will proceed to printing. The main problem i am having lies only at this part

Main Question :

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

I did a check before i tried if else just to make sure the value exist, however, even after it detects that it has a value, this line will throw an error NullReference

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

FULL Code just in case:

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

Edited : Data.getFullItemDetail only returns 1 int value.

    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 but... too many 'if' bro, this code burn my eyes – alessandro Nov 11 '14 at 09:00
  • i did cut half of the coding down haha. the full code is for reference only. – Salehin Suhaimi Nov 11 '14 at 09:03
  • productList can be null, productList[j + 1]["Number"] can be null, then Data.getFullItemDetail, we don't know what do, please reformat your question, cut all except relevant code please, atm is not very clear – alessandro Nov 11 '14 at 09:29
  • I'm sorry for the vagueness of the question. the relevant code is posted as Main Question. What i need it to do is to check the next value of the array as soon as it enters the loop, because the next item will decide what the current item will do. But if I check the last array, my j + 1 will do the obvious, throw the exception. – Salehin Suhaimi Nov 12 '14 at 03:28

1 Answers1

0

productList[j+1]["Number"] is null, so at the second line an exception is thrown (because you can't call null.InnerText), caught and then you set numCheck = string.Empty.

The next line you test for if (numCheck != null), which is true, because you just set it to string.Empty in the exception handler. You then call InnerText on productList[j+1]["Number"] again, which throws a NullReferenceException again.

Set breakpoints, walk through your code, inspect your variables, just as explained in What is a NullReferenceException and how do I fix it?.

Just change your if (numCheck != null) to if (productList[j+1]["Number"] != null).

Community
  • 1
  • 1
CodeCaster
  • 131,656
  • 19
  • 190
  • 236