question

CarloGoretti-1185 avatar image
0 Votes"
CarloGoretti-1185 asked JarvanZhang-MSFT edited

DataStore Method are not able to return null

Hey Guys,

Im having trouble with DataStore... I have a method that return a list and sometimes it should return null. Thats good but when it should return null then i get an exception that value cannot be null![120724-image.png][1]
[1]: /answers/storage/attachments/120724-image.png

Anyone who knows why it cant return null?
Here is my code:

         public async Task<List<Workouts>> GetWorkoutsAsync(string choosedDay, bool forceRefresh = false)
         {
             WorkoutDay = TodaysWorkout(choosedDay);
             if (WorkoutDay != null)
                 return await Task.FromResult(WorkoutDay.workouts);
             else
                 return null;
         }
    
         private WorkoutsByDay TodaysWorkout(string workoutday)
         {
             WorkoutsByDay test = null;
             for(int i = 0; i < allWorkoutDays.Count(); i++)
             {
                 if (test == null)
                 {
                     if (allWorkoutDays[i].WeekDay == workoutday)
                     {
                         test = allWorkoutDays[i];
    
                     }
                     else
                     {
                         test = null;
                     }
                 }
             }
    
             return test;
    
         }

And here is where i call the method:

     async Task TodaysWorkouts(string day)
     {

         var test = await DataStore.GetWorkoutsAsync(day, true);

     }

If this one return null then i get the exception... But if it dosent return null then it works...

dotnet-csharp
image.png (8.5 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
1 Vote"
Viorel-1 answered

Did you determine which part of your program raises the exception?

Maybe you can return an empty list instead of null.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks alot for that help! It worked! :)

0 Votes 0 ·