Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4265756
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:40:03+00:00 2026-05-21T06:40:03+00:00

I am currently trying to validate my ASP.NET form. I need to be to

  • 0

I am currently trying to validate my ASP.NET form. I need to be to sure that the user has entered a password of at least 5 characters.

I have done a check to make sure that something is valid using the following code:

else if (Request.Form["txtPassword"] == "")
{}

I am then checking that the characters is not less than 5 by using the following code:

if (Request.Form["txtPassword"].Length < 5)
{}

However, when I run the form and submit it, instead of it displaying the error to the user about the password length, I keep on getting an error from Visual Studio. Before I have tried to submit the form it displays:

Object reference not set to an instance of an object.

This error is only displayed when I am checking the length not if I am checking the String is empty.

Thanks for any help you can provide.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T06:40:04+00:00Added an answer on May 21, 2026 at 6:40 am

    You have a null reference exception. Request.Form[“txtPassword”] returns null. This is essentiall what is happening:

    string myString = null;
    
    // This is valid comparison because myString is a string object.
    // However, the string is not *empty*, it is null, thus it has *no* value.
    if(myString == "")
    {
        DoSomething();
    }
    
    // This is not acceptable, as null.Length has no meaning whatsoever.
    // The expression compiles because the variable is of type string,
    // However, the string is null, so the "Length" property cannot be called.
    if(myString.Length < 5)
    {
        DoSomethingElse();
    }
    

    You cannot access the length of a null string. Try using this instead:

    var myString = Request.Form["txtPassword"];
    
    if(!String.IsNullOrEmpty(myString) && myString.Length < 5)
    {
        DoSomething();
    }
    

    The next question, however, is WHY is it null? Perhaps you have inaccurately named the associated form input to something other than “txtPassword”, or perhaps the data is not being sent via POST.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently trying to validate that an Oracle db_link is actually pointing to
I am currently trying to validate a form server side, the way it works
I'm currently trying to validate fields without having an ActiveRecord::Base inheritance. My model stores
Currently trying at add ajax to a site, after much reading I discovered that
Im currently trying to downlaod a audio track from a WCF, i need some
I am currently trying to write some code that will accept some FTP details,
I have a DataAnnotationValidator that I created. I am currently trying to test it
I am currently trying to validate a textbox, so only letters (a-Z) can be
I am currently trying to hook up jQuery UI dialog so that I may
I am currently trying to validate some front-end values with some server-side methods (as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.