0

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?

  • It would help if you indicated exactly *where* the error is occurring? Have you tried debugging this? What variable is null and throwing the exception? – rory.ap Oct 19 '16 at 14:07
  • @rory.ap `errProv.SetError(c, String.Empty);` <-- this is a line where it trows exception. I'm not good at debugging. I think the error comes from `errProv.ClearErrors(txtUsername, txtPassword);` this line –  Oct 19 '16 at 14:10
  • is it really working code? I can see if (controls!=null & controls.Length>0) where only one & would not work you should have &&. – Ankit Oct 19 '16 at 14:22
  • It definitely works as I tried with other method and the code is exactly the same. –  Oct 19 '16 at 14:25
  • 1
    The C# language normally provides the guarantee that a NullReferenceException is raised at the call site. But that does not work in the specific case of an extension method, you get the exception in the method itself. You'll need to practice debugging, but it doesn't make it that hard to help you see that the errProv variable is null. Just hover the mouse over the names to see what they contain. That the variable name is an exact match with the extension method argument name is another hint for the underlying mistake. Usual name resembles "this.errorProvider1". – Hans Passant Oct 19 '16 at 14:36

1 Answers1

0

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?

Community
  • 1
  • 1
rory.ap
  • 34,009
  • 10
  • 83
  • 174
  • using mHotRes.DesktopPresentation.Extensions; this is where I define to use it from. I did not do anything else in the other class for validation and it works. –  Oct 19 '16 at 14:21
  • you are right in the login form designer it says `private System.Windows.Forms.ErrorProvider errProv;` <-- its never assigned and it will always have its default value as null. How do I assign it then? –  Oct 19 '16 at 14:35
  • You can assign it in the constructor for your form. – rory.ap Oct 19 '16 at 14:41
  • when I try to do that it says that login form already contains definition for errProv –  Oct 19 '16 at 14:44
  • Use the existing definition. Don't define a new one. `errProv = new ...` – rory.ap Oct 19 '16 at 14:46
  • `public ErrorProviderExtension errProv;` <-- is this the correct way of doing it as it gives me error that cannot declare a variable of static type `ErrorProviderExtension` –  Oct 19 '16 at 14:53
  • No, because you are re-defining an existing variable `errProv` that you have already defined somewhere else. Just use the existing instance of `errProv` and instantiate it like I said above. Don't do `public ErrorProviderExtension errProv;` – rory.ap Oct 19 '16 at 15:00