Can't catch NullReferenceException while explicitly defined for that
-
Hello fellow members. Im trying to understand why my code is not catching NullReferenceException and i cant figure it out. I'm using this code to run async function
private async Task SearchMostLessViewsAsync(DateTime dateFrom, DateTime dateTo, int amountOfNumbersSelected, int frequencyOption, int fromDrawNumber, int toDrawNumber)
{
Microsoft.AppCenter.Analytics.Analytics.TrackEvent($"{typeof(FragmentDrawsNumbersFrequency).Name}.{nameof(SearchMostLessViewsAsync)}",
new Dictionary()
{
{nameof(dateFrom), dateFrom.ToString()},
{nameof(dateTo), dateTo.ToString()},
{nameof(amountOfNumbersSelected), amountOfNumbersSelected.ToString()},
{nameof(frequencyOption), frequencyOption.ToString()},
{nameof(fromDrawNumber), fromDrawNumber.ToString()},
{nameof(toDrawNumber), toDrawNumber.ToString()},
}
);
((MainActivity)Activity).DisplayLoadingMessage(true, GetString(Resource.String.Common_SearchTitle),
GetString(Resource.String.Common_SearchMessage));//ApplicationState.ChangeCancellationTokenSource(new CancellationTokenSource()); var task = Task.Run(async () => { var textResult = await SearchLeast(dateFrom, dateTo, amountOfNumbersSelected, frequencyOption, fromDrawNumber, toDrawNumber).ConfigureAwait(false); if (!string.IsNullOrEmpty(textResult)) { UpdateHistoryList(textResult); DatabaseFunctions.SaveHistoryList(HistoryList, Settings.DrawsNumbersFrequencyHistoryListViewKey); ((MainActivity)Activity).ShowSearchResults(textResult); } }, ApplicationState.GetCancellationToken()); // Pass same token to Task.Run. try { await task.ConfigureAwait(false); } catch (TaskCanceledException tce) { Console.WriteLine($"{nameof(TaskCanceledException)} thrown with message: {tce.Message}"); } catch (System.ObjectDisposedException ode) { Console.WriteLine($"{nameof(System.ObjectDisposedException)} thrown with message: {ode.Message}"); } catch (System.OperationCanceledException e)
-
Hello fellow members. Im trying to understand why my code is not catching NullReferenceException and i cant figure it out. I'm using this code to run async function
private async Task SearchMostLessViewsAsync(DateTime dateFrom, DateTime dateTo, int amountOfNumbersSelected, int frequencyOption, int fromDrawNumber, int toDrawNumber)
{
Microsoft.AppCenter.Analytics.Analytics.TrackEvent($"{typeof(FragmentDrawsNumbersFrequency).Name}.{nameof(SearchMostLessViewsAsync)}",
new Dictionary()
{
{nameof(dateFrom), dateFrom.ToString()},
{nameof(dateTo), dateTo.ToString()},
{nameof(amountOfNumbersSelected), amountOfNumbersSelected.ToString()},
{nameof(frequencyOption), frequencyOption.ToString()},
{nameof(fromDrawNumber), fromDrawNumber.ToString()},
{nameof(toDrawNumber), toDrawNumber.ToString()},
}
);
((MainActivity)Activity).DisplayLoadingMessage(true, GetString(Resource.String.Common_SearchTitle),
GetString(Resource.String.Common_SearchMessage));//ApplicationState.ChangeCancellationTokenSource(new CancellationTokenSource()); var task = Task.Run(async () => { var textResult = await SearchLeast(dateFrom, dateTo, amountOfNumbersSelected, frequencyOption, fromDrawNumber, toDrawNumber).ConfigureAwait(false); if (!string.IsNullOrEmpty(textResult)) { UpdateHistoryList(textResult); DatabaseFunctions.SaveHistoryList(HistoryList, Settings.DrawsNumbersFrequencyHistoryListViewKey); ((MainActivity)Activity).ShowSearchResults(textResult); } }, ApplicationState.GetCancellationToken()); // Pass same token to Task.Run. try { await task.ConfigureAwait(false); } catch (TaskCanceledException tce) { Console.WriteLine($"{nameof(TaskCanceledException)} thrown with message: {tce.Message}"); } catch (System.ObjectDisposedException ode) { Console.WriteLine($"{nameof(System.ObjectDisposedException)} thrown with message: {ode.Message}"); } catch (System.OperationCanceledException e)
The exception is referring to this method:
SearchMostLessViewsAsync
. That's not anywhere in the code you're showing here. -
Hello fellow members. Im trying to understand why my code is not catching NullReferenceException and i cant figure it out. I'm using this code to run async function
private async Task SearchMostLessViewsAsync(DateTime dateFrom, DateTime dateTo, int amountOfNumbersSelected, int frequencyOption, int fromDrawNumber, int toDrawNumber)
{
Microsoft.AppCenter.Analytics.Analytics.TrackEvent($"{typeof(FragmentDrawsNumbersFrequency).Name}.{nameof(SearchMostLessViewsAsync)}",
new Dictionary()
{
{nameof(dateFrom), dateFrom.ToString()},
{nameof(dateTo), dateTo.ToString()},
{nameof(amountOfNumbersSelected), amountOfNumbersSelected.ToString()},
{nameof(frequencyOption), frequencyOption.ToString()},
{nameof(fromDrawNumber), fromDrawNumber.ToString()},
{nameof(toDrawNumber), toDrawNumber.ToString()},
}
);
((MainActivity)Activity).DisplayLoadingMessage(true, GetString(Resource.String.Common_SearchTitle),
GetString(Resource.String.Common_SearchMessage));//ApplicationState.ChangeCancellationTokenSource(new CancellationTokenSource()); var task = Task.Run(async () => { var textResult = await SearchLeast(dateFrom, dateTo, amountOfNumbersSelected, frequencyOption, fromDrawNumber, toDrawNumber).ConfigureAwait(false); if (!string.IsNullOrEmpty(textResult)) { UpdateHistoryList(textResult); DatabaseFunctions.SaveHistoryList(HistoryList, Settings.DrawsNumbersFrequencyHistoryListViewKey); ((MainActivity)Activity).ShowSearchResults(textResult); } }, ApplicationState.GetCancellationToken()); // Pass same token to Task.Run. try { await task.ConfigureAwait(false); } catch (TaskCanceledException tce) { Console.WriteLine($"{nameof(TaskCanceledException)} thrown with message: {tce.Message}"); } catch (System.ObjectDisposedException ode) { Console.WriteLine($"{nameof(System.ObjectDisposedException)} thrown with message: {ode.Message}"); } catch (System.OperationCanceledException e)
As Pete has said, the error isn't in the code you show, nor is the method that throws the error directly called in any of that code. Run your program in the debugger and when it fails, it will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, the debugger will stop before the error, and let you examine what is going on by stepping through the code looking at your values. But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
As Pete has said, the error isn't in the code you show, nor is the method that throws the error directly called in any of that code. Run your program in the debugger and when it fails, it will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, the debugger will stop before the error, and let you examine what is going on by stepping through the code looking at your values. But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
Richard? Are you saying I'm a complete and utter Richard? ;P
-
Richard? Are you saying I'm a complete and utter Richard? ;P
No I'm saying I'm a complete and utter pillock ... :laugh: Sorry ... fixed. :-O
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
As Pete has said, the error isn't in the code you show, nor is the method that throws the error directly called in any of that code. Run your program in the debugger and when it fails, it will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, the debugger will stop before the error, and let you examine what is going on by stepping through the code looking at your values. But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
oh yes you are right, sorry i have used the wrong part of the code. i edit the question again. I cant reproduce the bug in my device, it is happening in around 2% of the devices that is being used, this code is from an app that is available from google play already
-
Richard? Are you saying I'm a complete and utter Richard? ;P
what means to be a Richard? :) i didnt get the joke
-
what means to be a Richard? :) i didnt get the joke
Griff called me Richard. He's edited the post to use my name now.
-
what means to be a Richard? :) i didnt get the joke
I made a mistake, and referred to Pete as Richard, is all. No joke, just a mistake on my part.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
I made a mistake, and referred to Pete as Richard, is all. No joke, just a mistake on my part.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
oh i though it had a deeper meaning i didn't get :)
-
Hello fellow members. Im trying to understand why my code is not catching NullReferenceException and i cant figure it out. I'm using this code to run async function
private async Task SearchMostLessViewsAsync(DateTime dateFrom, DateTime dateTo, int amountOfNumbersSelected, int frequencyOption, int fromDrawNumber, int toDrawNumber)
{
Microsoft.AppCenter.Analytics.Analytics.TrackEvent($"{typeof(FragmentDrawsNumbersFrequency).Name}.{nameof(SearchMostLessViewsAsync)}",
new Dictionary()
{
{nameof(dateFrom), dateFrom.ToString()},
{nameof(dateTo), dateTo.ToString()},
{nameof(amountOfNumbersSelected), amountOfNumbersSelected.ToString()},
{nameof(frequencyOption), frequencyOption.ToString()},
{nameof(fromDrawNumber), fromDrawNumber.ToString()},
{nameof(toDrawNumber), toDrawNumber.ToString()},
}
);
((MainActivity)Activity).DisplayLoadingMessage(true, GetString(Resource.String.Common_SearchTitle),
GetString(Resource.String.Common_SearchMessage));//ApplicationState.ChangeCancellationTokenSource(new CancellationTokenSource()); var task = Task.Run(async () => { var textResult = await SearchLeast(dateFrom, dateTo, amountOfNumbersSelected, frequencyOption, fromDrawNumber, toDrawNumber).ConfigureAwait(false); if (!string.IsNullOrEmpty(textResult)) { UpdateHistoryList(textResult); DatabaseFunctions.SaveHistoryList(HistoryList, Settings.DrawsNumbersFrequencyHistoryListViewKey); ((MainActivity)Activity).ShowSearchResults(textResult); } }, ApplicationState.GetCancellationToken()); // Pass same token to Task.Run. try { await task.ConfigureAwait(false); } catch (TaskCanceledException tce) { Console.WriteLine($"{nameof(TaskCanceledException)} thrown with message: {tce.Message}"); } catch (System.ObjectDisposedException ode) { Console.WriteLine($"{nameof(System.ObjectDisposedException)} thrown with message: {ode.Message}"); } catch (System.OperationCanceledException e)
You're making the assumption that the method that is throwing the null reference is inside your task. What's interesting is that you have failed to apply any forms of input guards to SearchMostLessViewsAsync and you're assuming you actually have values coming in. It wouldn't surprise me if the null reference you are seeing occurred in this call:
Microsoft.AppCenter.Analytics.Analytics.TrackEvent($"{typeof(FragmentDrawsNumbersFrequency).Name}.{nameof(SearchMostLessViewsAsync)}", new Dictionary() { {nameof(dateFrom), dateFrom.ToString()}, {nameof(dateTo), dateTo.ToString()}, {nameof(amountOfNumbersSelected), amountOfNumbersSelected.ToString()}, {nameof(frequencyOption), frequencyOption.ToString()}, {nameof(fromDrawNumber), fromDrawNumber.ToString()}, {nameof(toDrawNumber), toDrawNumber.ToString()}, } );
If any of those is null, you'll get a null reference because you're calling
ToString()
on it. The bottom line is that you shouldn't make assumptions about where exceptions are occurring; look at the whole part of the code. -
You're making the assumption that the method that is throwing the null reference is inside your task. What's interesting is that you have failed to apply any forms of input guards to SearchMostLessViewsAsync and you're assuming you actually have values coming in. It wouldn't surprise me if the null reference you are seeing occurred in this call:
Microsoft.AppCenter.Analytics.Analytics.TrackEvent($"{typeof(FragmentDrawsNumbersFrequency).Name}.{nameof(SearchMostLessViewsAsync)}", new Dictionary() { {nameof(dateFrom), dateFrom.ToString()}, {nameof(dateTo), dateTo.ToString()}, {nameof(amountOfNumbersSelected), amountOfNumbersSelected.ToString()}, {nameof(frequencyOption), frequencyOption.ToString()}, {nameof(fromDrawNumber), fromDrawNumber.ToString()}, {nameof(toDrawNumber), toDrawNumber.ToString()}, } );
If any of those is null, you'll get a null reference because you're calling
ToString()
on it. The bottom line is that you shouldn't make assumptions about where exceptions are occurring; look at the whole part of the code.hello, those values cannot ever be null from the way im setting them app and also from the crash reports they are not null, they have a value, the crash comes later. some times this code doesnt crash. some times it does. even in the devices that crash it doesnt crash always. From what i understand my biggest problem is that i dont try catch inside the task and i loose exceptions because of that. I have added a try catch inside the task and now i see that even in my device thaat never crash sometimes i get an error about ssl. The correct way is to check all those values if they are null in the beggining of the function even if by the way are initialized always have value?