How can i get my application to accept null value as valid

I have a C#  application with a variable  selectedPeriod.mnthyear

I need to have the variable accept both regular values and Null

The test  script (attached)  is ok when there is a value.

When the value of the variable  is null  the application  reports an error as attached

What is wrong with my test syntax for   selectedPeriod.mnthyear  when it is null ?


Olukayode OluwoleSystems AnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPRetiredCommented:
I would use a try/catch block to trap these errors.
Chinmay PatelChief Technology NinjaCommented:
You should use Nullable Types
double? pi = 3.14;
char? letter = 'a';

int m2 = 10;
int? m = m2;

bool? flag = null;

// Array of nullable type:
int?[] arr = new int?[10];

Open in new window

OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Gustav BrockCIOCommented:
Don't check for a property of selectedPeriod but for the object itself, thus:

If (selectedPeriod == null)
// <snip>

Open in new window

Chris StanyonWebDevCommented:
Couple of points to clarify here.

When you are doing this:

if (selectedPeriod.mnthyear == null)

You are looking to check if the mnthyear property of the selectedPeriod object is null. The error you're getting is telling you that the selectedPeriod object is null. Because that's null, it throws and error and never actually gets to check the mnthyear property (you can't check the property of a null).

You should use the null-conditional operator here (the question mark), like so:

if (selectedPeriod?.mnthyear == null)

Now your check will return null if selectedPeriod is null. If it's not, then it will check the mnthyear. This will prevent the error you're getting, and match null if either selectedPeriod or mnthyear is null.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Olukayode OluwoleSystems AnalystAuthor Commented:
A big thank you to all the experts.

I believe any of the suggestions might have worked.

The solution from Chris Stanyon requiring only a  ?   appears easiest to implement

Once again thanks  for the help
Gustav BrockCIOCommented:
But you don't use the property mnthyear in this part of the code, so easiest ..?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.