Windows Phone 8 – Debug and Fix ‘System. Reflection .Target Invocation Exception’ error

wp8

Source : Dailymobile.net

As a beginner developer, debugging can quickly take the joy away from learning a new language/platform. And there’s  little information on debugging Windows Phone 8 Applications. The error titled above (targetinvocatoexception) did take a considerable amount of time on my part to detect and fix.
Note :  As of writing this article, I have ~4weeks experience developing C# apps for wp8 using Visual Studio. Point being I’m a beginner and this post is generally meant for beginners/intermediate c#/wp8 developers.

Scenario.

I integrated Scoreloop into my game application and load a players’ rank whenever  the leaderboard button is clicked. Given that I had been testing with my device (which had already posted scores to the board), I always got a rank without errors.

Unknown to me, I had failed to check for the condition when rank returns a null value because no score had been posted. I just requested rank and tried to set my text boxes and variables with the result.

However, each time I downloaded the app and installed from the App store, it crashed immediately (null rank returned because no score had been posted by that user).

This error NEVER occurred on my test device (because it has a rank value already). The problem is even more difficult to solve because there is no way to directly view (using visual studio debugger) the errors generated by the version downloaded from App Store, making it impossible to triangulate the source of the error from over 700 lines of code on that page. The closest bet is to inspect stack traces from crash count reports on the developer center . But the details provided there are  still not specific enough , especially for newbie developers .

Debugging the Problem

I read online that wp8 apps are recompiled after upload to the app store and some localization optimizations are performed (which sometimes lead to errors) . Thus, I tried reformatting any double values returned by the leaderboard api (by forcing the US english language culture) but still no results. App still crashed after waiting for 2hours to get through the beta certification process.
Then I realized that the error may be occurring because a new installation creates a new leaderboard player id (different from the player id on my test device) and there might be difficulty establishing this id. So I deleted my device leaderboard entry and discovered that I could reproduce the error on my test device! Super, I could now see the error message being generated when the application crashed in debug mode.

targetinvocation

This was a great starting point, but not enough details still.

Using Visual Studio Watch Window to Get Exception Details

To get more details, I used the Visual Studio watch window to inspect the details of the exception. To open the Watch window, the debugger must be running or in break mode. From the Debug menu, choose Windows, then Watch, and click on Watch1, Watch2, Watch3, or Watch4.
In the “watch” window, type $exception and press enter. This provides a large amount of details on the components of the exception.

watchwindow2

Specifically, pay attention to the innerException variable as this was what gave me the most insight .

And Voila!! ..  problem found

– _innerException {System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
at System.SZArrayHelper.get_Item[T](Int32 index)
at System.Linq.Enumerable.ElementAt[TSource](IEnumerable`1 source, Int32 index)
at TrivMonkey.LeaderBoardPage.rankcon_ScoresLoaded(Object sender, RequestControllerEventArgs`1 e)
at  .[T](EventHandler`1 , Object , T )} System.Exception {System.ArgumentOutOfRangeException}

From my little programming knowledge, I realized that this must have to do with an array that was being accessed with a forbidden index. In my case, I was accessing a null rank index. Immediately I added a test for null rank, I was able to prevent further crashes.

Summary

When you get the System.Reflection.TargetInvocationException error, a great place to begin the debug process would be to use the watch window. Also note that many different situations can trigger this error (not only the index out of range situation) but following the debugging steps above can really help identify your idiosyncratic problem.
Hope this is helpful to you.

About Vykthur

Mobile and Web App Developer and Researcher. Passionate about learning, teaching, and recently - writing.
This entry was posted in Developer Tips, Programming, Tutorials, Windows Phone 8 and tagged , , , . Bookmark the permalink.