Reputation: 1677
I have made a condition that if variable is null. But still this code breaks the app because of null variable.
CODE:
var page = IsolatedStorageSettings.ApplicationSettings["qsPage"];
var hash = IsolatedStorageSettings.ApplicationSettings["Ayath"];
if (page == null && hash == null)
// or if i use if (page == null || hash == null)
{
MessageBox.Show("No Bookmark has been saved !");
}
else
{
NavigationService.Navigate(new Uri("/myWeb.xaml?Page=" + page + "&id=" + hash, UriKind.Relative));
}
Upvotes: 0
Views: 106
Reputation: 1163
Using &&
in the if condition, the page can be null but the hash no, so it go to the else clausule and, as the page is null, it fail.
Try replacing &&
for ||
This way, everytime page OR hash is null, it show the message box.
Updated: As the page and hash variables are string, you need to test them using String.IsNullOrWhiteSpace, This way:
var page = IsolatedStorageSettings.ApplicationSettings["qsPage"];
var hash = IsolatedStorageSettings.ApplicationSettings["Ayath"];
if (string.IsNullOrWhiteSpace(page.ToString()) || string.IsNullOrWhiteSpace(hash.ToString()))
{
MessageBox.Show("No Bookmark has been saved !");
}
else
{
NavigationService.Navigate(new Uri("/myWeb.xaml?Page=" + page + "&id=" + hash, UriKind.Relative));
}
Upvotes: 1
Reputation: 6590
You have to check IsolatedStorageSettings.ApplicationSettings["qsPage"];
is returning null or any value. try this.
object pagedata = null,hashdata = null;
if(IsolatedStorageSettings.ApplicationSettings.Contains("qsPage") && IsolatedStorageSettings.ApplicationSettings.Contains("Ayath"))
{
var page = IsolatedStorageSettings.ApplicationSettings["qsPage"];
var hash = IsolatedStorageSettings.ApplicationSettings["Ayath"];
pagedata = page;
hashdata = hash;
}
if (pagedata == null && hashdata == null)
// or if i use if (page == null || hash == null)
{
MessageBox.Show("No Bookmark has been saved !");
}
else
{
NavigationService.Navigate(new Uri("/myWeb.xaml?Page=" + pagedata + "&id=" + hashdata, UriKind.Relative));
}
Upvotes: 1
Reputation: 16705
Try:
if (page == null || hash == null)
Instead. You're query requires both variables to be null.
Upvotes: 1