locked
System.NullReferenceException

    Question

  • public class MediaClass : ViewModelBase { private MediaElement mediaplay; public MediaElement MediaPaly { get { return mediaplay; } set { Set("MediaPlay", ref mediaplay, value); } } }


     public class MainViewModel : ViewModelBase
        {
            private MediaClass mediaTest;
            public MediaClass MediaTest
            {
                get { return mediaTest; }
                set {Set("MediaTest",ref mediaTest,value); }
            }
            
            public MainViewModel()
            {
                mediaTest = new MediaClass();
                MediaPlayCommand = new RelayCommand(Play);
            }
            public RelayCommand MediaPlayCommand { get; private set; }
            public async void Play()
            {
                FileOpenPicker filePicker = new FileOpenPicker();
                filePicker.SuggestedStartLocation = PickerLocationId.VideosLibrary;
    
                filePicker.FileTypeFilter.Add(".wmv");
                filePicker.FileTypeFilter.Add(".mp4");
                filePicker.ViewMode = PickerViewMode.Thumbnail;
    
                StorageFile localVideo = await filePicker.PickSingleFileAsync();
                
                if (localVideo != null)
                {
                    // Open the selected file and set it as the MediaElement's source
                    IRandomAccessStream stream = await localVideo.OpenAsync(FileAccessMode.Read);
                    this.MediaTest.MediaPaly = MediaTest.MediaPaly;
                    MediaTest.MediaPaly.SetSource(stream, localVideo.ContentType);
    
                }
            }
            
        }
     x:Class="MediaTest.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:MediaTest"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" DataContext="{Binding Main, Mode=OneWay, Source={StaticResource Locator}}">
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    	 <MediaElement x:Name="VideoSource" HorizontalAlignment="Left" Height="564" 
                          VerticalAlignment="Top" Width="1201" AreTransportControlsEnabled="True"
                          Margin="96,37,0,0" Source="{Binding MediaTest.MediaPaly.Source}"   
                          />
            <Button Height="45" Margin="861,651,0,72" Width="99" Content="Open File" 
                    Command="{Binding MediaPlayCommand, Mode=OneWay}"/>
        </Grid>

    MediaTest.MediaPaly.SetSource(stream, localVideo.ContentType); // System.NullReferenceException
    media Element Binding

    Thursday, November 06, 2014 5:40 AM

Answers

  • Thanks for your code, and I get to know what's the problem is, your MediaPlay is null, and how could you assign some stream to a null object?

    I'm not familiar with GalaSoft MvvmLight which is a third party code, but the code you are using quite strange. but definitely looks like your MediaPlay cannot get the correct MediaElement from front end.

    I think it could be better for you to ask how GalaSoft Mvvm works.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, November 11, 2014 7:10 AM
    Moderator

All replies

  • Hi SENTILSEBASTIAN,

    System.NullReferenceException seems to be either your MediaTest.MediaPaly, stream or contentType is null while setSource for the MedieElement. Could you try to set a breakpoint here to see which one is null?

    But I guess the biggest possibility might be the command did not assign the correct source to the correct MediaElement object but I'm not able to see the whole code so I cannot tell what exactly the problem is. If you could share us a demo we can debug for you.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, November 07, 2014 1:50 AM
    Moderator
  • Friday, November 07, 2014 3:03 AM
  • using GalaSoft.MvvmLight;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Windows.UI.Xaml.Controls;
    
    namespace MediaTest.Model
    {
        public class MediaClass : ViewModelBase
        {
            private MediaElement mediaplay;
            public MediaElement MediaPaly
            {
                get { return mediaplay; }
                set { Set("MediaPlay", ref mediaplay, value); }
            }
        }
    }

    using GalaSoft.MvvmLight;
    using GalaSoft.MvvmLight.Command;
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading.Tasks;
    using Windows.Storage;
    using Windows.Storage.Pickers;
    using Windows.Storage.Streams;
    using MediaTest.Model;
    using Windows.UI.Xaml.Controls;
    
    
    namespace MediaTest.ViewModel
    {
        public class MainViewModel : ViewModelBase
        {
            private MediaClass videoSource;
            public MediaClass VideoSource
            {
                get { return videoSource; }
                set { Set("VideoSource", ref videoSource, value); }
            }
    
            public MainViewModel()
            {
                videoSource = new MediaClass();
                MediaPlayCommand = new RelayCommand(Play);
            }
            public RelayCommand MediaPlayCommand { get; private set; }
            public async void Play()
            {
                FileOpenPicker filePicker = new FileOpenPicker();
                filePicker.SuggestedStartLocation = PickerLocationId.VideosLibrary;
    
                filePicker.FileTypeFilter.Add(".wmv");
                filePicker.FileTypeFilter.Add(".mp4");
                filePicker.ViewMode = PickerViewMode.Thumbnail;
    
                StorageFile localVideo = await filePicker.PickSingleFileAsync();
    
                if (localVideo != null)
                {
                    // Open the selected file and set it as the MediaElement's source
                    IRandomAccessStream stream = await localVideo.OpenAsync(FileAccessMode.Read);
                    this.VideoSource.MediaPaly = VideoSource.MediaPaly;
                   //  VideoSource.MediaPaly.Source = new Uri(localVideo.Path);
                     VideoSource.MediaPaly.SetSource(stream, localVideo.ContentType);
    
                }
            }
    
        }
    }

    using GalaSoft.MvvmLight.Ioc;
    using Microsoft.Practices.ServiceLocation;
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace MediaTest.ViewModel
    {
        public class ViewModelLocator
        {
            public MainViewModel Main
            {
                get
                {
                    return ServiceLocator.Current.GetInstance<MainViewModel>();
                }
            }
              static ViewModelLocator()
              {
    
                  SimpleIoc.Default.Register<MainViewModel>();
                  ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
              }
        }
    }

    <Application
        x:Class="MediaTest.App"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:MediaTest"
        
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        xmlns:vm="using:MediaTest.ViewModel">
    
        <Application.Resources>
            <ResourceDictionary>
    
                <vm:ViewModelLocator x:Key="Locator"
                                 d:IsDataSource="True" />
    
            </ResourceDictionary>
        </Application.Resources>
    </Application>
    using MediaTest.ViewModel;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices.WindowsRuntime;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;
    
    // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
    
    namespace MediaTest
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
    
            
        }
    }

    <Page
        x:Class="MediaTest.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:MediaTest"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" DataContext="{Binding Main, Mode=OneWay, Source={StaticResource Locator}}">
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <MediaElement x:Name="VideoSource" HorizontalAlignment="Left" Height="564" 
                          VerticalAlignment="Top" Width="1201" AreTransportControlsEnabled="True"
                          Margin="96,37,0,0" DataContext="{Binding MediaTest.MediaPaly, Mode=OneWay}"   
                          />
            <Button Height="45" Margin="861,651,0,72" Width="99" Content="Open File" 
                    Command="{Binding MediaPlayCommand, Mode=OneWay}"/>
        </Grid>
    </Page>



    Friday, November 07, 2014 3:05 AM
  • Thanks for your code, and I get to know what's the problem is, your MediaPlay is null, and how could you assign some stream to a null object?

    I'm not familiar with GalaSoft MvvmLight which is a third party code, but the code you are using quite strange. but definitely looks like your MediaPlay cannot get the correct MediaElement from front end.

    I think it could be better for you to ask how GalaSoft Mvvm works.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, November 11, 2014 7:10 AM
    Moderator