When it occurs An unhandled exception of type "'System.Reflection.TargetInvocationException' occurred in System.Windows.ni.dll" inWindows Phone
Asked Answered
P

3

12

Am creating a WP8 application which uses Web Service to fetch,create,update & delete data & displaying it.
Now the problem is that my application crashes by throwing

An unhandled exception of type "'System.Reflection.TargetInvocationException' occurred in System.Windows.ni.dll" inWindows Phone

enter image description here

There is no stack trace for this exception & i am stuck with this one for hours. And i noticed that this exception occurs whenever calling the service more frequently than normal but i didn't get the actual reason.

It is really helpful to know
1.What type of exception is this ?
2.At what condition this will happen ?
3.How we can handle the app crash because of this exception?

Portland answered 28/11, 2013 at 9:36 Comment(7)
@Olivier it throws on Application_UnhandledException event at App.xaml.csPortland
I mean: TargetInvocationException is just an exception that wraps the actual exception that has been thrown... what is the exception into yourException.InnerException property ?Sight
@Sight there is no Inner exception associated with that . Please see the image i added now.Portland
In the "watch" window, type $exception... what does it yields ?Sight
Alternatively, you may go to "Debug->Exceptions" and check "Common Language Runtime Exceptions" on "Throw" column. (and "Use unhandled" as well)Sight
You are right . gotta an inner exception object says " at MyApp.ViewModels.CreateViewModel.<Create20Images>d__61.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__0(Object state) " Message : Object reference not set to an instance of an object.Portland
Am in doubt that why a very common exception like this 'Object reference not set to an instance of an object' is invoked on the app's unhandledException event?Portland
S
14

Your comment

You are right . gotta an inner exception object says " at MyApp.ViewModels.CreateViewModel.d__61.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.AsyncMethodBuilderCore.b__0(Object state) " Message : Object reference not set to an instance of an object.

just shows that somewhere, you're invoking an async method which is not awaited: when a method returns a task, always await it.

Your inner exception should have an inner exception (i.e. $exception.InnerException.InnerException whiches stacktrace will show you the location of your NullReferenceException)

UnhandledException event is invoked when an exception in your code has not been handled by your code, and the app doesnt know how to handle it. By default, it makes your app crash. However, you can prevent your app from crashing in those cases. See this to know more about it.

To fix your issue "the clean way", you will have to find the place where your code is not awaited, and fix it. ie somewhere you will find:

myObject.DoSomethingAsync(); // DoSomethingAsync() returns a task.

Change it :

try
{
   await myObject.DoSomethingAsync(); // DoSomethingAsync() returns a task.
}catch(Exception ex)
{
   // display error message or whatever
}

[edit] this will handle the error, but what you really want to fix is the cause of your nullref exception. I dont have that much clues, but it looks like a thread concurrency issue.

Sight answered 28/11, 2013 at 10:34 Comment(1)
This text should be bold "when a method returns a task, always await it.". Saved me from hours of debugging because I was looking somewhere else in my code.Sassenach
H
3

1.What type of exception is this?

this exception will occur when you are trying to access a w/s and it will become overloaded.

2.At what condition this will happen?

Business logic is complicated or db lock or nonresponsive

3.How we can handle the app crash because of this exception?

to handle it I recommend to do better the performance of that w/s or you can use recursive call but it will not solve the issue I think

Homesick answered 28/11, 2013 at 10:11 Comment(1)
It only occurs when the app sends service request frequently. In normal running of app this one is not occurs at all. Am afraid , Will it makes my app fail in Windows Phone Store submission?Portland
N
0

Or InvokeRequired when use Task

delegate void SetDataSourceHandler(DataTable data); 

public void SetDataSource(dDataTable data)
{
    if (gvData.InvokeRequired)
            {
                gvData.Invoke(new SetDataSourceHandler(SetDataSource), new object[] { data });
                return;
            }
            nodosDataTableBindingSource.DataSource = data;

        }

async Task ProcesarMensajes()
{
...
 SetDataSource( GetList(nodes));
}
Noto answered 5/12, 2016 at 19:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.