UWP App unknown exception - debugging

I am making a media app. In which I load playlists and then when I play from one playlist, it plays fine, but I have the same file in another playlist, It gives an exception, of system dubugger like this
I am using following code when gridview selection changes to play the file.
private async void PlaylistGridView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
if (PlaylistGridView.SelectedIndex != -1)
{
CurrentlyPlayingVideo = (VideoFile)PlaylistGridView.SelectedItem;
CurrentlyPlayingFile = CurrentlyPlayingVideo.File;
var s = await CurrentlyPlayingFile.OpenReadAsync();
var sq = me;
me.SetSource(s, CurrentlyPlayingFile.ContentType);
}
}
catch { }
}
the message in e.Message is 'Object refrence is not set to instance of object',
I surely know it is null type error, but problem is i dnt know which object is null!!, I am even using try catch blocks, but still its giving that exception in the picture above, and not telling me the object which is null, until i dnt know which object is null, how can i fix the error?
EDIT :
I tried to run in release mode with visual studio and this is the error i get even before trying to play anything.

Object reference is not set to instance of an object
I had seen this error when I build an uwp too. If I'm not mistaken, any of your Control (Button , Textblock , etc) is called by any of your method (including event) before they have been initialized to the Page Check the calling once again

Related

Xamarin Forms OnConfigurationChanged - System.ArgumentNullException: Value cannot be null Parameter name: enumerable

I'm working on my first Xamarin Forms application. I've run into an issue that I cannot figure out how to debug. There's a null value in the theme. But there aren't a lot of indicators as to what may be missing. If I catch this exception and ignore it, there doesn't seem to be any issue with the app execution.
When I change the orientation of the phone, I get this exception:
{System.ArgumentNullException: Value cannot be null.
Parameter name: enumerable
at Xamarin.Forms.Internals.EnumerableExtensions.IndexOf[T] (System.Collections.Generic.IEnumerable`1[T] enumerable, T item) [0x00003] in D:\a\1\s\Xamarin.Forms.Core\EnumerableExtensions.cs:79
at Xamarin.Forms.Application.OnRequestedThemeChanged (Xamarin.Forms.AppThemeChangedEventArgs args) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Application.cs:172
at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.OnConfigurationChanged (Android.Content.Res.Configuration newConfig) [0x0001e] in D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\FormsAppCompatActivity.cs:90
at App1.Droid.MainActivity.OnConfigurationChanged (Android.Content.Res.Configuration newConfig) [0x00002] in C:\source\repos\App1\App1\App1.Android\MainActivity.cs:30 }
I see it comes from the internal Forms code. Any idea what it is trying to enumerate over?
The comment asks for the code at line MainActivity.cs:30
The code at line 30 is base.OnConfigurationChanged(newConfig);
The point is that the exception happens in the internal Xamarin.Forms code and I have no idea what the enumeration is supposed to represent.
public override void OnConfigurationChanged(Configuration newConfig)
{
try
{
base.OnConfigurationChanged(newConfig);
}
catch (ArgumentNullException ex)
{
string m = ex.Message;
}
}
I had the same problem and rolling Xamarin.Forms back to version 4.6.0.726 fixed it.
I have submitted a bug report on Xamarin.Forms Github https://github.com/xamarin/Xamarin.Forms/issues/10733

Xamarin.iOS: MobileService.InvokeApiAsync skips

I am developing an app using Xamarin.Forms to re-use as much code as possible.
Specifically, I have a static class that manages all the POST/GET requests to my Azure Web Server.
Everything single call works fantastically except for one call - this one:
public async static Task<Models.UserParkPosition> GetUserParkPositionForCurrentUserAsync()
{
var body = new JArray { App.User.Id };
var test = await AzureMobileServiceClient.Instance.MobileService.InvokeApiAsync<JArray, Models.UserParkPosition>(ConnectionsAPI, body, HttpMethod.Get, null);
return test;
}
The method above is called when the user presses a button - specifically like this:
private async Task OnGoingToTheParkClicked(object sender, EventArgs e)
{
bool success = false;
var already = await viewModel.AlreadyHavePendingRequestAsync());
Console.WriteLine("TEST");
throw new Exception();
}
When the debugger hits the "var test" line mentioned, above.... nothing happens. The code doesn't deadlock, the UI is still responsive, but the code never returns. I never see the "TEST" word and the exception is not even thrown... what happens??
All my other APIs are called in the same way, and are working correctly.
OTHER INFO:
- Same code works on UWP and Android
- The GET request arrives at the webservice, which responds in a timely fashion (< 1 sec)
Thanks to anyone who might help or even point me in the right direction!
Found the issue - InvokeApiAsync throws an exception which is not catched anywhere from any calling thread. It somehow just disappears without causing any error nor device log, nor crash. Weird, but that's how the world goes.

Windows Moible 6.5 SDK GPS Sample Bugged

I cannot seem to find a good solution for this issue online. I have a device that is running Windows Embedded Handheld 6.5. I run the solution located at below
C:\Program Files (x86)\Windows Mobile 6.5.3 DTK\Samples\PocketPC\CS\GPS
I deploy the code to my device, not an emulator, and the code breaks with a null reference exception at
Invoke(updateDataHandler);
The solution ive seen recommends changing this to below
BeginInvoke(updateDataHandler);
But now the code breaks at Main with NullRefreceException.
Application.Run(new Form1());
Has anyone found a solution for this?
Did you alter the code? updateDataHandler is initialized in Form_Load:
private void Form1_Load(object sender, System.EventArgs e)
{
updateDataHandler = new EventHandler(UpdateData);
so that object will not be NULL. But there are other annoyances with the code, especially the Samples.Location class. You may instead use http://www.hjgode.de/wp/2010/06/11/enhanced-gps-sample-update/ as a starting point and the older one: http://www.hjgode.de/wp/2009/05/12/enhanced-gps-sampe/
The main issue with the sample is that it does not use a callback (delegate) to update the UI. If an event handler is fired from a background thread, the handler can not directly update the UI. Here is what I always use to update the UI from a handler:
delegate void SetTextCallback(string text);
public void addLog(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.txtLog.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(addLog);
this.Invoke(d, new object[] { text });
}
else
{
txtLog.Text += text + "\r\n";
}
}

Can't create handler inside thread which has not called Looper.prepare() [xamarin]

So i don't actually have a question because i've already solved it, but in case someone else runs into this issue it's always nice to have a neat solution.
And while there is a plentitude of "Can't create handler inside thread which has not called Looper.prepare()" - questions there is none tagged with xamarin. (so theirs is all java and i had 0 matches for "Can't create handler inside thread which has not called Looper.prepare() [xamarin]")
The issue is generated because You tried to do work on UI from other thread. If you want to change UI, Must call UI changes from UI thread.
Xamarin Android:
activity.RunOnUiThread(() =>
{
// Write your code here
});
Xamarin IOS:
nsObject.BeginInvokeOnMainThread(() =>
{
// Write your code here
});
Xamarin Forms:
Device.BeginInvokeOnMainThread(() =>
{
// Write your code here
});
public static class PageExtensions
{
public static Task<bool> DisplayAlertOnUi(this Page source, string title, string message, string accept, string cancel)
{
TaskCompletionSource<bool> doneSource = new TaskCompletionSource<bool>();
Device.BeginInvokeOnMainThread(async () =>
{
try
{
var result = await source.DisplayAlert(title, message, accept, cancel);
doneSource.SetResult(result);
}
catch (Exception ex)
{
doneSource.SetException(ex);
}
});
return doneSource.Task;
}
}
Finally i had a case for using TaskCompletionSource to solve an issue.
Very similar actually happend to me. Previous evening I was developing and testing my app. Next morning, from other computer I got the exception you described. I was remembered from a official Xamarin.Forms documentation that sometimes a bin and obj folder removal solves lot of issues.
I did exactly the same, removed my bin and obj folder from my shared Xamarin.Forms library and also from Xamarin.Android library.
The strange exception disappeared.

Out of Range exception while accessing the files in Windows 8 Metro app

Below is the code I used to access the asset file for a metro app I am working on.
async void readFileFromDisk (string fileName, string fileType)
{
string fileContent;
StorageFile file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(fileName);
using (IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read))
{
using (DataReader dataReader = new DataReader(readStream))
{
UInt32 numBytesLoaded = await dataReader.LoadAsync((UInt32)readStream.Size);
fileContent = dataReader.ReadString(numBytesLoaded);
}
}
This code is run in the handler for Loaded event for the page. I am currently getting an exception saying "Value does not fall in range". The error occurs at the first line itself, where I try to get storagefile handle from the installation folder.
On debugging, the fileName string comes out to be Null. I guess, I should be moving the code to some event which is fired at a later stage in page lifecycle, but can't seem to figure out what is the best place to do it. Suggestions??
P.S. I need to read this file before any interaction from user, as it reads the data for the level, that user will be interacting with.
Edit:
Missed a couple things.
The below function is called from the handler for loaded event.
void Game_Loaded(object sender, RoutedEventArgs e)
{
//read all level files to the strings
readFileFromDisk("//Assets/Levels/Start" + selectedLevel + ".txt", "Start");
This handler basically calls above function for different file paths, in similar manner. The string selected level is static variable, while the fileName string is created from the same.
Edit 2:
Found the issue, but solution is still far away. The return type of readFileFromDist method is causing trouble. Changed it to Task, and this part works fine, but I get "Object reference not set to an instance" error. Tried to convert Game_Loaded event handler to async too, to use await operators, but that gives me compiler error for "wrong return type".
SO, I tried removing async completely, but I guess I can't do that. There is no way to open files without using async function. So, I essentially need a way to call the readFileFromDisk function, using await, and continue with rest of the code execution once the task is completed. Something like, "IsCompleted" event for the awaited calls for the function.
Solved! Needed to use "ms:appx///Assets/filename.txt" instead of "//Assets/filename.txt".

Resources