Object reference not set to an instance of an object.

Posted by: reflaherty on 8 September 2017, 1:18 pm EST

  • Posted 8 September 2017, 1:18 pm EST

    After I remove all of the characters from a cell, I get Null-Reference error when performing a GetValue on that cell.


    Below is the code:

    <FONT size=2>

    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>private</FONT></FONT><FONT size=2> </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>static</FONT></FONT><FONT size=2> </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>string</FONT></FONT><FONT size=2> GetSpreadStringValue(</FONT><FONT color=#2b91af size=2><FONT color=#2b91af size=2>SheetView</FONT></FONT><FONT size=2> sv, </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>int</FONT></FONT><FONT size=2> iRow, </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>int</FONT></FONT><FONT size=2> iCol)


    {


    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>try

    </FONT></FONT><FONT size=2>

    {


    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>if</FONT></FONT><FONT size=2> (</FONT><FONT color=#2b91af size=2><FONT color=#2b91af size=2>String</FONT></FONT><FONT size=2>.IsNullOrEmpty(sv.GetValue(iRow, iCol).ToString()) == </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>true</FONT></FONT><FONT size=2>)


    {


    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>return</FONT></FONT><FONT size=2> </FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>""</FONT></FONT><FONT size=2>;


    }


    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>else

    </FONT></FONT><FONT size=2>

    {


    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>return</FONT></FONT><FONT size=2> sv.GetValue(iRow, iCol).ToString();


    }


    }


    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>catch</FONT></FONT><FONT size=2> (</FONT><FONT color=#2b91af size=2><FONT color=#2b91af size=2>NullReferenceException</FONT></FONT><FONT size=2> ex)


    {


    </FONT><FONT color=#2b91af size=2><FONT color=#2b91af size=2>ErrorHandling</FONT></FONT><FONT size=2>.LogError(ex, </FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"Row: "</FONT></FONT><FONT size=2> + iRow.ToString() + </FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>" - Column: "</FONT></FONT><FONT size=2> + iCol.ToString());


    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>return</FONT></FONT><FONT size=2> </FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>""</FONT></FONT><FONT size=2>;


    }


    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>catch</FONT></FONT><FONT size=2> (</FONT><FONT color=#2b91af size=2><FONT color=#2b91af size=2>Exception</FONT></FONT><FONT size=2> ex)


    {


    </FONT><FONT color=#2b91af size=2><FONT color=#2b91af size=2>ErrorHandling</FONT></FONT><FONT size=2>.LogError(ex, </FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"Row: "</FONT></FONT><FONT size=2> + iRow.ToString() + </FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>" - Column: "</FONT></FONT><FONT size=2> + iCol.ToString());


    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>return</FONT></FONT><FONT size=2> </FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>""</FONT></FONT><FONT size=2>;


    }


    }

    </FONT>

     


    Below is in part the error message generated: 


    Error Type: NullReferenceException
    When: 6/20/2008 11:53:53 AM
    Description: Object reference not set to an instance of an object.
    Source: OrderWizProspects
    Stack Trace:    at OrderWizProspects.frmConvertProspectImport.GetSpreadStringValue(SheetView sv, Int32 iRow, Int32 iCol) in C:\Documents and Settings\REF.BLCC\My Documents\Visual Studio 2008\Projects\Windows\OrderWizNet\OrderWizProspects\frmConvertProspectImport.cs:line 51
    Target Site: System.String GetSpreadStringValue(FarPoint.Win.Spread.SheetView, Int32, Int32)

  • Replied 8 September 2017, 1:18 pm EST

    Hello,


    If an object is null, you can not call the ToString method. This is what is causing the exception. You should check the value returned by GetValue is not null before calling the ToString method.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels