Exception thrown when it shouldn't be

software is fun
    private async Task<long> InsertCustomerRecord(CreateCustomerModel model)
    {

        Int64 phoneNumber = 0;
        var isParsed = Int64.TryParse(model.PhoneNumber, out phoneNumber);
        if (!isParsed)
            phoneNumber = 0;
        var USPSAddressValidatedId = (int)model.USPSAddressValidated;
        try
        {
            IDataParameter[] parameters = new IDataParameter[]
            {
            new SqlParameter("@FirstName", model.FirstName.ToUpper()),
            new SqlParameter("@LastName", model.LastName.ToUpper()),
            //new SqlParameter("@MiddleInitial", model.MiddleInitial),
            new SqlParameter("@AddressLine1",model.AddressLine1.ToUpper() ?? ""),
            new SqlParameter("@AddressLine2",model.AddressLine2.ToUpper() ?? ""),
            new SqlParameter("@City",model.City.ToUpper()),
            new SqlParameter("@State",model.State.ToUpper()),
            new SqlParameter("@CountyCode",model.CountyCode.ToUpper()),
            new SqlParameter("@ZipCode",model.ZipCode),
            new SqlParameter("@DateOfBirth",model.DateOfBirth.ToShortDateString()),
            new SqlParameter("@Phone",phoneNumber),
            new SqlParameter("@Email",model.EmailAddress.ToUpper()??""),
            new SqlParameter("@PersonalRepresentative",model.CustomerClass.ToUpper()),
            new SqlParameter("@ExpiryDate", model.ExpirationDate),
            new SqlParameter("@CustomerClass", model.Customer_Class),
            new SqlParameter("@USPSAddressValidated",USPSAddressValidatedId ),
            new SqlParameter("@ImportFromLegacySystem", model.ImportFromLegacySystem)
            };
        
            return await InsertUpdateProcedure("RF_InsertCustomerCard", parameters);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            return 0;
        }
    }

The exception thrown is

{"Object reference not set to an instance of an object."}
    Data: {System.Collections.ListDictionaryInternal}
Caius Jard

If you have something that you suspect might be null:

model.EmailAddress
      ^^^^^^^^^^^^
   this might be null

You need to use the null propagating operator on it:

model.EmailAddress?.ToUpper() ?? ""
                  ^^

This means if it is null, then evaluation of the chain of operations will cease at the point that .EmailAddress returns null, and the sub-expression (model.EmailAddress?.ToUpper()) will resolve to null. It will then be picked up by the null coalescing operator ?? and turned into whatever you've put on the right hand side of the operator. If it's a constant, like "" then the whole expression is guaranteed to not be null

You can use these operators multiple times, on property and method return values:

thing.Prop?.Method() ?? thing.OtherProp?.OtherMethod()?.AnotherProp ?? "Constant"

There is also a null tolerant indexer if collections might be null:

//in case the collection is null, or the thing at index 0 is null, or its Property is null...
thing.Collection?[0]?.Property?.Something() ...
                ^^

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Break when exception is thrown

Exception thrown when parsing data

waitpid blocking when it shouldn't

Sending an email when an Exception is Thrown

Exception has been thrown by the target of an invocation thrown when scaffolding a controller

What happens when an exception is thrown?

Inconsistent behavior: no exception is thrown in the List<T>.Sort method when called in a foreach loop

Shouldn't an undefined variable throw an exception on checking with #if?

Why app doesn't crash when exception is thrown

Keep on receiving an argument of of range exception when there shouldn't be one?

No logs when exception is thrown in bot

Function running when it shouldn't

Why Compensate method doesn't Call when a consumer thrown exception in MassTransit RouterSlip

Null Pointer Exception when there shouldn't be?

Webmatrix site - Security Exception - Shouldn't be there

jQuery Toggling when it shouldn't

Exception isn't thrown when nil inserted in NSDictionary

Exception thrown when calling getFriends

Why isn't an exception thrown when the hash value doesn't exist?

Code running when it shouldn't?

Task not in a faulted state when an exception is thrown

An img that moves when shouldn't

How to assert 'Exception B' when "Exception A: Exception B" is thrown?

When exception thrown, CORS is lost

Picture changing when it shouldn't

$_SESSION is echoing when it shouldn't?

Why can't I access the response content when the Symfony HTTP Client encounters an error, and an exception is thrown instead?

Argument passed to when() is not a mock! exception thrown with Spring Boot project which doesn't have @SpringBootApplication/main class

Permission denied when it shouldn't be