0

I have an SQL database that I'm extracting data out of. However, some of that data isn't measured and is therefore NULL. I've been able to avoid some null values using this method:

if (dataItem.Feature1!= null) { 
 trainingCategorial.Feature1= (float)dataItem.Feature1; 
}
else { 
 trainingCategorial.Feature1= float.NaN; 
}

Where dataItem is the my dataset which I'm iterating through with foreach.

foreach (dataItem in dataset)

However, now I have the same issue but with an equation:

if(dataItem.CalculateFeature2(afdKenm, (decimal)dataItem.Feature3) != null)
{
 trainingCategorial.Feature2 = 
 dataItem.CalculateFeature2(afdKenm, (decimal)dataItem.Feature3)  > 0;
}

The goal of the code is to create a boolean of Feature2. I've checked and it didn't seem as feature3 has any NULL values. My question lies in the way that my c# code doesn't even want to check the dataItem.CalculateFeatures2 before giving the error. It just immediately says that "Nullable object must have a value".

Can anyone help me with this? Thank you in advance!

4
  • 1
    What is Feature3? Why are you doing this (decimal) cast? Also the compiler, when it looks at your code it doesn't know what value Feature3 holds. How is it supposed to know what is in the dataset? You declared the field as nullable, and therefore it won't allow you to freely deal with it. Its a safety mechanism. So the real question you should ask yourself is: what is the intended behaviour if Feature3 actually is null? And then implement it. If there is no way for Feature3 to ever be null, then you might consider redaclaring the field/property as not nullable. Commented Jan 23, 2024 at 8:26
  • How did you check? Commented Jan 23, 2024 at 8:30
  • I used a simple if(=nul) than sum. But I found the issue. I was confused because I used this method beforehand in another code, but there I calculate Feature3 myself instead of extracting it from the database. In the database everything was indeed 0. Thank you! Commented Jan 23, 2024 at 9:35
  • OT you are (trying to) execute dataItem.CalculateFeature2(..) twice. I would store the result of the first execution in a variable and test/use that result - this may save some (potentially expensive) execution time Commented Jan 23, 2024 at 9:57

1 Answer 1

0

I tried to check if there were null values based on:

int count = 0;
if (Feature3 = null){
count++;}

However, this did not work and there were indeed null values in my database. I was confused as I had already used a similar method beforehand, but there weren't any null values in that dataset. Eventually I just calculated Feature3 myself, and now the code indeed works.

Thank you for your help! And for other people struggeling with this issue: If you're using an

if(dataItem != Null)

Use a breakpoint in the if function, because if it doesn't go in there in your debug than you also know that there are NULL values in your dataset and you can handle them appropiately.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.