Reputation: 2474
I have a program that has a simple yes or no Toggle Button and I can toggle switch between the two buttons. However, when I click either one of them, it crashes the program and breaks into this:
// An unhandled exception has occurred; break into the debugger
Debugger.Break();
I have debugged where I think is causing the problem here on ToggleButton_Tap Event, since I tapped on it, but instead ended the program without debugging through this:
C# Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Windows.Media; // added to support SolidColorBrush, FontWeights, etc...
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Data_Query.Resources;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Text.RegularExpressions;
namespace Data_Query
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
//dateOfBirthPicker.SelectedDate = DateTime.Today;
}
DateTime dtN; // var to set current time
bool clicked = false; // returns true if DateTimePicker was clicked at least once; otherwise false.
bool validation = false; // check validation upon submitting the data query
int countChange = 0; // check how many times it went through data picker value_changed
String msg; // print the query results
// user's date of birth by using a date picker
private void dateOfBirthPicker_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
{
countChange++; // increment after everytime user changes DateTimePicker
clicked = true; // set value to true when user had changed DateTimePicker
}
// validates first, beforing sending the user's information into a database query
private void submitButton(object sender, RoutedEventArgs e)
{
//------------------------------------FIRST NAME-------------------------------//
if(string.IsNullOrWhiteSpace(firstNameTB.Text) || firstNameTB.Text == "")
{
firstNameTBL.Text = "First Name: *";
firstNameTBL.Foreground = new SolidColorBrush(Colors.Red);
firstNameTBL.FontWeight = FontWeights.Bold;
validation = false;
}
else if(!(string.IsNullOrWhiteSpace(firstNameTB.Text) || firstNameTB.Text == ""))
{
// set back to default layout
this.firstNameTBL.ClearValue(TextBlock.ForegroundProperty);
this.firstNameTBL.ClearValue(TextBlock.FontWeightProperty);
this.firstNameTBL.Text = "First Name:";
validation = true;
}
//------------------------------------LAST NAME-------------------------------//
if(string.IsNullOrWhiteSpace(lastNameTB.Text) || lastNameTB.Text == "")
{
lastNameTBL.Text = "Last Name: *";
lastNameTBL.Foreground = new SolidColorBrush(Colors.Red);
lastNameTBL.FontWeight = FontWeights.Bold;
validation = false;
}
else if(!(string.IsNullOrWhiteSpace(lastNameTB.Text) || lastNameTB.Text == ""))
{
// set back to default layout
this.lastNameTBL.ClearValue(TextBlock.ForegroundProperty);
this.lastNameTBL.ClearValue(TextBlock.FontWeightProperty);
this.lastNameTBL.Text = "Last Name:";
validation = true;
}
//------------------------------------EMAIL ADDRESS-------------------------------//
if(string.IsNullOrWhiteSpace(emailAddressTB.Text) || emailAddressTB.Text == "")
{
emailAddressTBL.Text = "Email Address: *";
emailAddressTBL.Foreground = new SolidColorBrush(Colors.Red);
emailAddressTBL.FontWeight = FontWeights.Bold;
validation = false;
}
else if (!(string.IsNullOrWhiteSpace(emailAddressTB.Text) || emailAddressTB.Text == ""))
{
// set back to default layout
this.emailAddressTBL.ClearValue(TextBlock.ForegroundProperty);
this.emailAddressTBL.ClearValue(TextBlock.FontWeightProperty);
this.emailAddressTBL.Text = "Email Address:";
validation = true;
}
//------------------------------------DATE OF BIRTH-------------------------------//
dtN = DateTime.Now; // initialize DateTime.Now to be use in value comparsion with DateTimePicker
if(countChange >= 1)
{
clicked = true;
}
else clicked = false;
if(!clicked) // false when user hasn't changed or set the DateTimePicker_ValueChanged
{
dateOfBirthTBL.Text = "Date of Birth: *";
dateOfBirthTBL.Foreground = new SolidColorBrush(Colors.Red);
dateOfBirthTBL.FontWeight = FontWeights.Bold;
validation = false;
}
else if(clicked) // true when user has changed or set the DateTimePicker_ValueChanged
{
// set back to default layout
this.dateOfBirthTBL.ClearValue(TextBlock.ForegroundProperty);
this.dateOfBirthTBL.ClearValue(TextBlock.FontWeightProperty);
this.dateOfBirthTBL.Text = "Date of Birth:";
validation = true;
}
//------------------------------------GENDER-------------------------------//
if(maleRB.IsChecked == false && femaleRB.IsChecked == false)
{
genderTBL.Text = "Gender: *";
genderTBL.Foreground = new SolidColorBrush(Colors.Red);
genderTBL.FontWeight = FontWeights.Bold;
validation = false;
}
else if(!(maleRB.IsChecked == false && femaleRB.IsChecked == false))
{
// set back to default layout
this.genderTBL.ClearValue(TextBlock.ForegroundProperty);
this.genderTBL.ClearValue(TextBlock.FontWeightProperty);
this.genderTBL.Text = "Gender:";
validation = true;
}
//------------------------------------DISABILITY-------------------------------//
if (yesTBU.IsChecked == false && noTBU.IsChecked == false)
{
// message box and text font change or Not Specified
disabilityTBL.Text = "Do you have a disability? *";
disabilityTBL.Foreground = new SolidColorBrush(Colors.Red);
disabilityTBL.FontWeight = FontWeights.Bold;
validation = false;
}
else
{
// set back to default layout
this.disabilityTBL.ClearValue(TextBlock.ForegroundProperty);
this.disabilityTBL.ClearValue(TextBlock.FontWeightProperty);
this.disabilityTBL.Text = "Do you have a disability?";
validation = true;
}
if(validation) //if all passes, bring up the results
{
msg = String.Format("First Name: {0}\nLast Name: {1}\nEmail Address: {2}\nDate of Birth: {3}\nGender: {4}\nHas a Disability? {5}", firstNameTB.Text, lastNameTB.Text, emailAddressTB.Text, dateOfBirthPicker.Value, strRBItem, strTBItem);
queryResult();
}
else
{
MessageBox.Show("There are one or more invalid inputs, please go back and check your data thoroughly!", "Error!", MessageBoxButton.OK);
}
}
// display the data result and confirm
public bool queryResult()
{
if(true)
{
MessageBox.Show("Make sure your data below is correct.\n\n" + msg + "\n\nClick OK to confirm, otherwise CANCEL to re-enter your data!\n", "Confirm Your Data Query...", MessageBoxButton.OKCancel);
return true;
}
}
//------------------------------------DISABILITY-------------------------------//
// method to handle all Toggle Button tap events
private void ToggleButton_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
// if currently tapped button is yesTBU
if(sender == yesTBU)
{
noTBU.IsChecked = !yesTBU.IsChecked; // set noTBU to opposite value of yesTBU
}
// else if currently tapped button is noTBU
else
{
yesTBU.IsChecked = !noTBU.IsChecked; // set yesTBU to opposite value of noTBU
}
// makes dynamic changes immediately in UI
if(yesTBU.IsChecked == false && noTBU.IsChecked == false)
{
// message box and text font change or Not Specified
disabilityTBL.Text = "Do you have a disability? *";
disabilityTBL.Foreground = new SolidColorBrush(Colors.Red);
disabilityTBL.FontWeight = FontWeights.Bold;
}
else
{
// set back to default layout
this.disabilityTBL.ClearValue(TextBlock.ForegroundProperty);
this.disabilityTBL.ClearValue(TextBlock.FontWeightProperty);
this.disabilityTBL.Text = "Do you have a disability?";
}
}
// clear all user's information inputs and reset values
private void resetButton(object sender, RoutedEventArgs e)
{
firstNameTB.Text = string.Empty;
lastNameTB.Text = string.Empty;
emailAddressTB.Text = string.Empty;
dateOfBirthPicker.Value = DateTime.Now;
maleRB.IsChecked = false;
femaleRB.IsChecked = false;
clicked = false;
yesTBU.IsChecked = false;
noTBU.IsChecked = false;
countChange = 0;
// set back to default layout
this.firstNameTBL.ClearValue(TextBlock.ForegroundProperty);
this.firstNameTBL.ClearValue(TextBlock.FontWeightProperty);
this.firstNameTBL.Text = "First Name:";
this.lastNameTBL.ClearValue(TextBlock.ForegroundProperty);
this.lastNameTBL.ClearValue(TextBlock.FontWeightProperty);
this.lastNameTBL.Text = "Last Name:";
this.emailAddressTBL.ClearValue(TextBlock.ForegroundProperty);
this.emailAddressTBL.ClearValue(TextBlock.FontWeightProperty);
this.emailAddressTBL.Text = "Email Address:";
this.dateOfBirthTBL.ClearValue(TextBlock.ForegroundProperty);
this.dateOfBirthTBL.ClearValue(TextBlock.FontWeightProperty);
this.dateOfBirthTBL.Text = "Date of Birth:";
this.genderTBL.ClearValue(TextBlock.ForegroundProperty);
this.genderTBL.ClearValue(TextBlock.FontWeightProperty);
this.genderTBL.Text = "Gender:";
this.disabilityTBL.ClearValue(TextBlock.ForegroundProperty);
this.disabilityTBL.ClearValue(TextBlock.FontWeightProperty);
this.disabilityTBL.Text = "Do you have a disability?";
}
// supply a method to allow you to use the SIP Enter key to dismiss the SIP
private void TextBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
this.Focus();
}
}
// firstNameTB Textbox to dynamically check validation
private void firstNameTB_LostFocus(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(firstNameTB.Text) || firstNameTB.Text == "" || !Regex.IsMatch(firstNameTB.Text, @"^[A-Z]{1}[a-z]+$"))
{
firstNameTBL.Text = "First Name: *";
firstNameTBL.Foreground = new SolidColorBrush(Colors.Red);
firstNameTBL.FontWeight = FontWeights.Bold;
}
else
{
// set back to default layout
this.firstNameTBL.ClearValue(TextBlock.ForegroundProperty);
this.firstNameTBL.ClearValue(TextBlock.FontWeightProperty);
this.firstNameTBL.Text = "First Name:";
}
}
// lastNameTB Textbox to dynamically check validation
private void lastNameTB_LostFocus(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(lastNameTB.Text) || lastNameTB.Text == "" || !Regex.IsMatch(lastNameTB.Text, @"^[A-Z]{1}[a-z]+$"))
{
lastNameTBL.Text = "Last Name: *";
lastNameTBL.Foreground = new SolidColorBrush(Colors.Red);
lastNameTBL.FontWeight = FontWeights.Bold;
}
else
{
// set back to default layout
this.lastNameTBL.ClearValue(TextBlock.ForegroundProperty);
this.lastNameTBL.ClearValue(TextBlock.FontWeightProperty);
this.lastNameTBL.Text = "Last Name:";
}
}
// emailAddressTB Textbox to dynamically check validation
private void emailAddressTB_LostFocus(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(emailAddressTB.Text) || emailAddressTB.Text == "" || !(emailAddressTB.Text.Contains("@") && emailAddressTB.Text.Contains(".")))
{
emailAddressTBL.Text = "Email Address: *";
emailAddressTBL.Foreground = new SolidColorBrush(Colors.Red);
emailAddressTBL.FontWeight = FontWeights.Bold;
}
else
{
// set back to default layout
this.emailAddressTBL.ClearValue(TextBlock.ForegroundProperty);
this.emailAddressTBL.ClearValue(TextBlock.FontWeightProperty);
this.emailAddressTBL.Text = "Email Address:";
}
}
String strRBItem; // variable to extract the content value of the selected Radio Button and display in msg
private void maleRB_Checked(object sender, RoutedEventArgs e)
{
strRBItem = (string)(sender as RadioButton).Content;
}
private void femaleRB_Checked(object sender, RoutedEventArgs e)
{
strRBItem = (string)(sender as RadioButton).Content;
}
String strTBItem; // variable to extract the content value of the selected Toggle Button and display in msg
private void yesTBU_Checked(object sender, RoutedEventArgs e)
{
strTBItem = (string)(sender as RadioButton).Content;
}
private void noTBU_Checked(object sender, RoutedEventArgs e)
{
strTBItem = (string)(sender as RadioButton).Content;
}
}
}
XAML Code:
<ToggleButton Name="yesTBU" Content="Yes" Tap="ToggleButton_Tap" Checked="yesTBU_Checked" Height="100" Width="150" Margin="0,583,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<ToggleButton Name="noTBU" Content="No" Tap="ToggleButton_Tap" Checked="noTBU_Checked" Height="100" Width="150" Margin="151,583,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
Upvotes: 0
Views: 553
Reputation: 69987
In order to fix your problem, you really need to learn some programming basics. Let the program crash, put your breakpoint in the Application_UnhandledException
handler and find out what the Exception
is... you said is was a NullReferenceException
, so that means that one of your objects is null
. Look in the StackTrace
to find the place in the code where the Exception
was actually thrown.
So now you know the offending line of code and what caused the crash, a null
reference, more accurately, your code trying to call a method on an object that is null
. You now have two options; if you can't guarantee that the object will never be null
, then handle the situation with a simple check for null
:
if (someObject != null) someObject.DoSomething();
If however, your object should never be null
, then simply debug your program, putting breakpoints in the relevant places in your code to see why the object is null
at that point and then fix that problem.
Upvotes: 2