Reputation:
I created a method to validate form. This method uses extension from another class. The same code work for validating other form but it prevents me from validation the logging form. The code is nearly the same but it does not work for this instance.
Here is my validation method:
public bool ValidateForm()
{
bool isValid = true;
errProv.ClearErrors(txtUsername, txtPassword);
if(String.IsNullOrEmpty(txtUsername.Text)) { errProv.SetError(txtUsername, LangRes.FieldIsRequired); isValid = false; }
if(String.IsNullOrEmpty(txtPassword.Text)) { errProv.SetError(txtPassword, LangRes.FieldIsRequired); isValid = false; }
return isValid;
}
This is my error provided class:
public static class ErrorProviderExtension
{
/// <summary>
/// To Clear all errors associated with controls
/// </summary>
/// <param name="errProv"></param>
/// <param name="controls"></param>
public static void ClearErrors(this ErrorProvider errProv, params Control[] controls)
{
if (controls!=null & controls.Length>0)
{
foreach(Control c in controls)
{
errProv.SetError(c, String.Empty);
}
}
}
}
The problem is that object reference not set to an instance of an object but i don't understand it that well if the other one is working without this problem I don't see why this one is giving me this exception. Can someone help?
Upvotes: 0
Views: 386
Reputation: 35338
It looks like you haven't instantiated errProv
. Where is that defined? Have you called its constructor anywhere?
You're attempting to use a method of errProv
(i.e. SetError
), but errProv
is null. You can't call a method on something that doesn't exist.
See this for more information: What is a NullReferenceException, and how do I fix it?
Upvotes: 0