I need to add a Null Reference, but know how

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<span lang="EN-US" style="font-family:Verdana,sans-serif; font-size:9pt I have built my own application based upon a sample.<br/>
Im trying to connect a small reference database to a listbox. When i run the<br/>
application I get a NullReferenceException error on the line " <span style="font-family:Verdana,sans-serif _applicationContext = (App.Current<br/>
as App).ApplicationDataContext;" in the Datahelper.cs see<br/>
the unterneath added code.<o:p></o:p>
<span lang="EN-US" style="font-family:Verdana,sans-serif; font-size:9pt Because i just started learning C# and Windows Phone<br/>
development I have no idea how to solve this error.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
System.NullReferenceException was unhandled<br/>
Message=NullReferenceException<br/>
StackTrace:<br/>
at DataSources.DataHelper.GetLists()<br/>
at Views.FirstPage.OnNavigatedTo(NavigationEventArgs e)<br/>
at Microsoft.Phone.Controls.PhoneApplicationPage.InternalOnNavigatedTo(NavigationEventArgs e)<br/>
at System.Windows.Navigation.NavigationService.RaiseNavigated(Object content, Uri uri, NavigationMode mode, Boolean isNavigationInitiator, PhoneApplicationPage existingContentPage, PhoneApplicationPage newContentPage)<br/>
at System.Windows.Navigation.NavigationService.CompleteNavigation(DependencyObject content, NavigationMode mode)<br/>
at System.Windows.Navigation.NavigationService.ContentLoader_BeginLoad_Callback(IAsyncResult result)<br/>
at System.Windows.Navigation.PageResourceContentLoader.BeginLoad_OnUIThread(AsyncCallback userCallback, PageResourceContentLoaderAsyncResult result)<br/>
at System.Windows.Navigation.PageResourceContentLoader.<>c__DisplayClass4.<BeginLoad>b__0(Object args)<br/>
at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean
verifyAccess, StackCrawlMark& stackMark)<br/>
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)<br/>
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)<br/>
at System.Delegate.DynamicInvokeOne(Object[] args)<br/>
at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)<br/>
at System.Delegate.DynamicInvoke(Object[] args)<br/>
at System.Windows.Threading.DispatcherOperation.Invoke()<br/>
at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)<br/>
at System.Windows.Threading.Dispatcher.OnInvoke(Object context)<br/>
at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)<br/>
at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)<br/>
at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FirstPage.xaml
<pre class="prettyprint <phone:PhoneApplicationPage
x:Class="Views.FirstPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True

<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="SelectionListItemTemplate
<Grid VerticalAlignment="Top" Height="auto
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<!--<TextBlock Text="Gemeente"
FontSize="10"
Grid.Row="0"/>-->
<TextBlock Text="{Binding Path=ProvincieName}"
FontSize="20"
Grid.Row="1"/>
<!--<TextBlock Text="Provincie"
FontSize="10"
Grid.Row="2"/>-->
<!--<TextBlock Text="{Binding GemeenteName}"
FontSize="20"
Grid.Row="3"/>-->
</Grid>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28
<TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>

<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0
<StackPanel>
<ListBox x:Name="SelectionBox" ItemsSource="{Binding}" ItemTemplate="{StaticResource SelectionListItemTemplate}"/>
</StackPanel>
</Grid>
</Grid>

<!--Sample code showing usage of ApplicationBar-->
<!--<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="MenuItem 1"/>
<shell:ApplicationBarMenuItem Text="MenuItem 2"/>
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>-->
</phone:PhoneApplicationPage>
[/code]
FirstPage.xaml.cs
<pre class="prettyprint namespace Views
{
public partial class FirstPage : PhoneApplicationPage
{
private readonly DataHelper _dataHelper;

public FirstPage()
{
InitializeComponent();

_dataHelper = new DataHelper();
}

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
DataContext = _dataHelper.GetLists();

base.OnNavigatedTo(e);
}
}
}[/code]
Datahelper.cs
<pre class="prettyprint namespace DataSources
{
public class DataHelper
{
#region Members
private readonly Random _rnd = new Random();
private readonly ApplicationDataContext _applicationContext;
#endregion

public DataHelper()
{
_applicationContext = (App.Current as App).ApplicationDataContext;
}

#region Overviews
public IEnumerable<ProvincieOverview> GetLists()
{
return _applicationContext.ProvincieOverviews.ToList();
}

public ProvincieOverview GetList(int listId)
{
var list = _applicationContext.ProvincieOverviews.SingleOrDefault(m => m.ProvincieId == listId);

return list;
}

#endregion
}
}
[/code]
ApplicationDataContext.cs
<pre class="prettyprint namespace DataSources
{
public class ApplicationDataContext : DataContext
{
// Specify the connection string as a static, used in main page and app.xaml.
public static string DBConnectionString = "Data Source = appdata:/DataSources/D66AppDB; File Mode = read only;";

// Pass the connection string to the base class.
public ApplicationDataContext(string connectionString)
: base(connectionString)
{ }

// Specify a single table for the to-do items.
public Table<GemeenteOverview> GemeenteOverviews;

// Specify a single table for the to-do items.
public Table<ProvincieOverview> ProvincieOverviews;
}
}[/code]
<br/>


View the full article
 
Back
Top