DaClownie
DaClownie
CC#
Created by DaClownie on 6/16/2024 in #help
NullReferenceException but checking for Null?
I click a button on my .NET MAUI UI to Create a new course. ContentPage loads, I enter my information. If I leave any of the entry fields blank, the program crashes... stating System.NullReferenceException Message=Object reference not set to an instance of an object.
private async void SaveCourseButton_Clicked(object sender, EventArgs e)
{
Course course = new Course(id, CourseName.Text, StartDate.Date, EndDate.Date, StatusPicker.SelectedIndex, InstructorName.Text, InstructorPhone.Text, InstructorEmail.Text, CourseNotes.Text, 0, 0);
var result = DataObjects.ValidateCourseInfo(activeTerm, course);
if (result.Length == 0)
{
SQLFunctions.AddNewCourse(course);
await Navigation.PopAsync();
}
else
{
await DisplayAlert("ERROR", result, "OK");
}
}

public static string ValidateCourseInfo(Term term, Course course)
{
if (course.CourseName.Length == 0 || course.CourseName is null)
{
return "Course must have a name.";
}
else if (course.EndDate.Date < course.StartDate.Date)
{
return "End date must be equal or greater than start date.";
}
else if (course.StartDate.Date < term.StartDate || course.StartDate.Date > term.EndDate || course.EndDate.Date < term.StartDate || course.EndDate.Date > term.EndDate)
{
return "Course start and end dates must fall within term start and end dates.";
}
else if (course.Status == -1)
{
return "Must pick a course status.";
}
else if (course.InstructorName.Length == 0 || course.InstructorName is null)
{
return "Instructor must have a name.";
}
...
else
{
return string.Empty;
}
}
private async void SaveCourseButton_Clicked(object sender, EventArgs e)
{
Course course = new Course(id, CourseName.Text, StartDate.Date, EndDate.Date, StatusPicker.SelectedIndex, InstructorName.Text, InstructorPhone.Text, InstructorEmail.Text, CourseNotes.Text, 0, 0);
var result = DataObjects.ValidateCourseInfo(activeTerm, course);
if (result.Length == 0)
{
SQLFunctions.AddNewCourse(course);
await Navigation.PopAsync();
}
else
{
await DisplayAlert("ERROR", result, "OK");
}
}

public static string ValidateCourseInfo(Term term, Course course)
{
if (course.CourseName.Length == 0 || course.CourseName is null)
{
return "Course must have a name.";
}
else if (course.EndDate.Date < course.StartDate.Date)
{
return "End date must be equal or greater than start date.";
}
else if (course.StartDate.Date < term.StartDate || course.StartDate.Date > term.EndDate || course.EndDate.Date < term.StartDate || course.EndDate.Date > term.EndDate)
{
return "Course start and end dates must fall within term start and end dates.";
}
else if (course.Status == -1)
{
return "Must pick a course status.";
}
else if (course.InstructorName.Length == 0 || course.InstructorName is null)
{
return "Instructor must have a name.";
}
...
else
{
return string.Empty;
}
}
23 replies
CC#
Created by DaClownie on 5/21/2024 in #help
New ContentPages in .net MAUI app don't have InitializeComponent() methods
I'm following a few guides to learn how to do Navigation in a .NET MAUI app which I need to apply for a project for school. All of the guides show creation of multiple ContentPages, assigning it as such in App.xaml.cs
MainPage = new NavigationPage(new TermPage());
MainPage = new NavigationPage(new TermPage());
I created a new ContentPage in the solution named TermPage but it doesn't have a InitializeComponent() and I'm unable to Build my project and send it to the Android Emulator. How come the guides have no issue with the creation of the InitializeComponent() method and mine does? And How do I fix it?
17 replies