0

We use Fortify to scan the code for our application. In one particular code as shown below

dr["test"] = GetTest(temp.Type!.Substring(0, 1));

Fortify throws null Deference error. So, I changed the code as shown below

dr["test"] = GetTest(temp.Type.Length > 0 ? temp.Type!.Substring(0, 1) : temp.Type);

though I am getting the same Null Deference error. I googled some codes related to null deference but I did not get the exact solution.

Dave
  • 5,108
  • 16
  • 30
  • 40
Zia Zee
  • 17
  • 6
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – madreflection Mar 29 '21 at 18:54

2 Answers2

1

If the problem is in temp.Type, you could try validating with IsNullOrEmpty. link

Juan Ozuna
  • 32
  • 4
0

This save SubString function might solve your problems:

///<summary>Get a save substring. If length is < 0, the remainder of the string is returned. Never returns NULL.</summary>
    public static string SaveSubStr( this string value, int offset, int length = -1 ) {
        if ( null == value || 0 == length ) return CS.Empty;
        if ( offset < 0 ) offset = 0;
        var l = value.Length;
        if ( 0 == l ) return value;
        if ( offset > l ) return CS.Empty;
        if ( length < 0 || offset + length > l ) length = l - offset;
        if ( length < 0 ) return CS.Empty;
        return value.Substring( offset, length );
    }
cskwg
  • 790
  • 6
  • 13