In my small, simple Forms application I'm experiencing a lot of Exeptions which I'm trying to handle. Some are apparently related to .NET and hence out of my control. But some are related to my code. Here's an example:
This code, which just reads the contents of a row of TextBoxes and instanciates another class if they're not empty, gives the following exception:
NullReferenceException occurred
Additional information: Object reference not set to an instance of an object.
Troubleshooting tips:
Check to determine if the object is null before calling the method.
Use the "new" keyword to create an object instance.
But I AM checking if it's null. I've got a try-catch for NullReferenceException, and I'm also checking for null or whitespace in the if-statement. What more can I do? Is this how it's supposed to be? Is the exception just a notification?
C#:
for (int i = 0; i < userPanel.RowCount * userPanel.RowCount - 1; i++) {
try {
if (!string.IsNullOrWhiteSpace(userPanel.GetControlFromPosition(1, i).Text)) {
instances.Add(new GCDriver(websiteTextBox.Text.ToString()));
SiteLogin.AuthWindow.LoginUser(userPanel.GetControlFromPosition(1, i).Text.ToString(),
userPanel.GetControlFromPosition(2, i).Text.ToString());
}
} catch (NullReferenceException) { }
}
This code, which just reads the contents of a row of TextBoxes and instanciates another class if they're not empty, gives the following exception:
NullReferenceException occurred
Additional information: Object reference not set to an instance of an object.
Troubleshooting tips:
Check to determine if the object is null before calling the method.
Use the "new" keyword to create an object instance.
But I AM checking if it's null. I've got a try-catch for NullReferenceException, and I'm also checking for null or whitespace in the if-statement. What more can I do? Is this how it's supposed to be? Is the exception just a notification?