-4

I have problem with my App i made choose button to select file from PC but i got exception if i didn't choose any files hope anyone can help thanks enter image description here

Edit: Code with Try/Catch blocks

openPicker.FileTypeFilter.Add(".wmv");
openPicker.FileTypeFilter.Add(".mp4"); 
openPicker.FileTypeFilter.Add(".wma"); 
openPicker.FileTypeFilter.Add(".mp3"); 
StorageFile file = await openPicker.PickSingleFileAsync(); 
var stream = (dynamic)null; 
try { 
      stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
} catch (Exception ex) { 
    Frame.Navigate(typeof(music)); // optional to back to choose page 
 }
Artur Peniche
  • 481
  • 6
  • 27
  • 1
    Post the actual code and error message in text, it is unclear what the text says in the picture. Judging by your title, you know there is a possibility that you won't select anything so you should add some error handling – Sayse Apr 29 '15 at 09:51
  • A first chance exception of type 'System.NullReferenceException' occurred in MadeYourDay.Windows.exe Additional information: Object reference not set to an instance of an object. this what exception says and i had use (try and catch) but fail – user3346249 Apr 29 '15 at 09:56

1 Answers1

-2
    var stream;
    StorageFile file;
    try{
        file = await openPicker.PickSingleFileAsync();
        stream = await file.OpenAsync(Windows.Storage.FileAcessMode.Read);
    }catch(Exception ex){
        //log or something
    }
    //the rest of the code
Artur Peniche
  • 481
  • 6
  • 27
  • got the same exception – user3346249 Apr 29 '15 at 10:01
  • @downvoter, care to explain? – Artur Peniche Apr 29 '15 at 10:25
  • maybe you could try catch {} instead of using a parameter. Or maybe you could add file variable on the try block, or even add a finally block after the catch block. can you show me how are you using the try/catch? – Artur Peniche Apr 29 '15 at 10:27
  • openPicker.FileTypeFilter.Add(".wmv"); openPicker.FileTypeFilter.Add(".mp4"); openPicker.FileTypeFilter.Add(".wma"); openPicker.FileTypeFilter.Add(".mp3"); StorageFile file = await openPicker.PickSingleFileAsync(); var stream = (dynamic)null; try { stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); } catch (Exception ex) { Frame.Navigate(typeof(music)); // optional to back to choose page } – user3346249 Apr 29 '15 at 10:29
  • music is other xaml page i just redirect to it – user3346249 Apr 29 '15 at 10:37
  • Please try to put StorageFile file = await openPicker.PickSingleFileAsync(); inside the try block. this is really wierd – Artur Peniche Apr 29 '15 at 10:42
  • i solved it already by another way if (null != file) { stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); media.SetSource(stream, file.Path); } thank you all for your efforts – user3346249 Apr 29 '15 at 10:42
  • glad you've fixed it – Artur Peniche Apr 29 '15 at 10:44
  • i just need other help from you if you don't mind i want to know how to make the same code but in windows phone 8.1 i try to make it 4 days ago but it fail as ( openPicker.PickSingleFileAsync() ) is not for windows phone hope you can help me with it @Artur Peniche – user3346249 Apr 29 '15 at 10:45
  • well mate i am not into windows phone but with a quick search i've found something similar with what you're facing [link]http://stackoverflow.com/questions/15357363/openfilepicker-not-working-on-windows-phone-8-specified-method-is-not-supported – Artur Peniche Apr 29 '15 at 10:53
  • i had other problem just found ... how i can keep the music file playing even i had move to other page ... i found that if i move to another page the music stop @ArturPeniche – user3346249 Apr 29 '15 at 11:11
  • since i don't have your code on sight https://social.msdn.microsoft.com/Forums/windowsapps/en-US/241ba3b4-3e2a-4f9b-a704-87c7b1be7988/how-to-continue-playing-media-after-navigating-to-other-pages-in-app?forum=winappswithcsharp – Artur Peniche Apr 29 '15 at 11:52