none
Error 'object reference not set instance of object' RRS feed

  • Question

  • I get this error during debugging if there is nothing in the refOther and it continues thru all of the other nested if statement. When I debug and hover over refOther the value is 'null'. How can I set this so if nothing is in refOther it doesn't hit the other nested if statements.

    foreach (DataRow dr in drs.Rows)
                {
                    // Lookup reason in Reference_OtherReason table
                    REFERENCE_OTHERREASON refOther = (from db in dc.REFERENCE_OTHERREASONs
                                                      where db.deleted_by == -1
                                                      && db.VALUE == dr["OtherReason"].ToString()
                                                      select db).FirstOrDefault();
    
                    if (float.Parse(dr["Contract"].ToString()) > 0)
                    {
    
                        hrsReg += double.Parse(dr["Contract"].ToString());
                    }
    
                    if (float.Parse(dr["Other"].ToString()) > 0)
                    {
                        if (dr["OtherReason"].ToString().ToLower().StartsWith("training"))
                        {
                            if (refOther.OTHERREASON_CUSTOMER == 1)
                            {
                                hrsMR += double.Parse(dr["Other"].ToString());
                            }
                            else
                            {
                                hrsReg += double.Parse(dr["Other"].ToString());
                            }
                        }
                        else if (dr["OtherReason"].ToString().ToLower().StartsWith("alt"))
                        {
                            if (dr["OtherReason"].ToString().ToLower() == "alt.job rainout hrs")
                            {
                                if (refOther.OTHERREASON_ALTJOBNUM == 1)
                                {
                                    hrsMR += double.Parse(dr["Other"].ToString());
                                }
                                else
                                {
                                    hrsReg += double.Parse(dr["Other"].ToString());
                                }
                            }
                            else
                            {
                                if (refOther.OTHERREASON_ALTJOBNUM == 1)
                                {
                                    hrsReg += double.Parse(dr["Other"].ToString());
                                }
                                else
                                {
                                    hrsMR += double.Parse(dr["Other"].ToString());
                                }
                                
                            }
                        }
                        else if (dr["OtherReason"].ToString().ToLower().StartsWith("rework"))
                        {
                            if (dr["OtherReason"].ToString().ToLower() == "Rework Rainout Hrs")
                            {
                                if (refOther.OTHERREASON_ALTJOBNUM == 1)
                                {
                                    hrsMR += double.Parse(dr["Other"].ToString());
                                }
                                else
                                {
                                    hrsReg += double.Parse(dr["Other"].ToString());
                                }
                            }
                            else
                            {
                                if (refOther.OTHERREASON_CUSTOMER == 1)
                                {
                                    hrsReg += double.Parse(dr["Other"].ToString()); // org
                                    //hrsMR += double.Parse(dr["Other"].ToString());
                                }
                                else
                                {
                                    hrsMR += double.Parse(dr["Other"].ToString()); // org
                                    //hrsReg += double.Parse(dr["Other"].ToString());
                                }
                            }
                        }
                        else if (dr["OtherReason"].ToString().ToLower().StartsWith("mentoring"))
                        {
                            hrsReg += double.Parse(dr["Other"].ToString()); // org
                        }
                    }
    
                    if (float.Parse(dr["Rain"].ToString()) > 0)
                    {
                        hrsMR += double.Parse(dr["Rain"].ToString());
                    }
    
    
                    if (float.Parse(dr["Travel"].ToString()) > 0)
                    {
                        hrsTR += double.Parse(dr["Travel"].ToString());
                    }
    
                }

    Monday, October 8, 2018 7:14 PM

All replies

  • Hi,

    if(refOther != null && ...)

    the second condition is just checked if refOther is not null, means you can use refOthers after that in the condition. But refOther != null has to stay on first position!

    Greetings, Chris

    Monday, October 8, 2018 7:33 PM
  • I'm sure what you mean exactly. Where does  if(refOther != null && ...)  go in the code and what goes after the &&..? Also the  code errors on this condition (bolded line)

      if (float.Parse(dr["Other"].ToString()) > 0)
                    {
                        if (dr["OtherReason"].ToString().ToLower().StartsWith("training"))
                        {
                            if (refOther.OTHERREASON_CUSTOMER == 1)
                            {
                                hrsMR += double.Parse(dr["Other"].ToString());
                            }
                            else
                            {
                                hrsReg += double.Parse(dr["Other"].ToString());
                            }
                        }




    • Edited by bootzilla Monday, October 8, 2018 7:55 PM
    Monday, October 8, 2018 7:51 PM
  • Hi,

    if (refOther != null && refOther.OTHERREASON_CUSTOMER == 1)

    but what is in case of else? Maybe you need a new if (refOther != null), and this if has to embrace the whole if else.

    Greetings, Chris

    Monday, October 8, 2018 7:56 PM
  • What is in case of Else? Assuming you are asking about this:

     else
                            {
                                if (refOther.OTHERREASON_CUSTOMER == 1)
                                {
                                    hrsReg += double.Parse(dr["Other"].ToString()); // org
                                    //hrsMR += double.Parse(dr["Other"].ToString());
                                }
                                else
                                {
                                    hrsMR += double.Parse(dr["Other"].ToString()); // org
                                    //hrsReg += double.Parse(dr["Other"].ToString());
                                }
                            }
                        }

    Not sure. I'm not the original author of this code. Looks like they want to check if the reason is 'Rework..' then check the Otherreason customer is set to 1.

    Monday, October 8, 2018 8:39 PM
  • Hmm ok,

    I would do it like this:

     else
                            {
                                if (refOther != null && refOther.OTHERREASON_CUSTOMER == 1)
                                {
                                    hrsReg += double.Parse(dr["Other"].ToString()); // org
                                    //hrsMR += double.Parse(dr["Other"].ToString());
                                }
                                else
                                {
                                    hrsMR += double.Parse(dr["Other"].ToString()); // org
                                    //hrsReg += double.Parse(dr["Other"].ToString());
                                }
                            }
                        }

    How I see, there are 5 locations where you have to add this to the if: refOther != null && 


    Greetings, Chris

    Monday, October 8, 2018 8:53 PM
  • But this is just to get it running, you should also find out why it is null:

    REFERENCE_OTHERREASON refOther = (from db in dc.REFERENCE_OTHERREASONs
                                                      where db.deleted_by == -1
                                                      && db.VALUE == dr["OtherReason"].ToString()
                                                      select db).FirstOrDefault();

    Monday, October 8, 2018 8:56 PM