Exception Details: System.NullReferenceException: Object reference not set to an instance of an object

Hi
Does anyone know why I get the error in the title, my code is below.

                               string strLocation, strCountry, strYear, strMessage, strTaken, strIDNumber;
            private void Page_Load(object sender, System.EventArgs e)
            {
                  strLocation = tbLocation.Text;
                  strCountry= ddlCountry.SelectedItem.Text;
                  strYear =  ddlYear.SelectedItem.Text;
                  strMessage =   tbMsg.Text;
                  strTaken = tbVisitorName.Text;
                  strIDNumber = Session.SessionID;
                  // Put user code to initialize the page here
            }

private void btnSubmit_Click(object sender, System.EventArgs e)
            {
                  MailMessage mail = new MailMessage();
                  mail.From = "picture@**.com";
                  mail.To = "picture@**.com";
                  mail.Subject = "Image";
                  mail.Body = "Country: strCountry"  + "\n" + "Location: strLocation" + "\n" + "Year: strYear" + "\n" + "Message: strMessage" + "\n" + "Taken by: strTaken";
                  mail.BodyFormat = MailFormat.Html;

                  string strdir = "C:\\Documents and Settings\\Administrator\\Desktop\\UploadImages\\";

                  string strfilename = Path.GetFileName(txtFile.PostedFile.FileName);

                  txtFile.PostedFile.SaveAs(strdir+strfilename);

                  mail.Attachments.Add(new MailAttachment(strdir+strfilename));

                  try
                  {
                        SmtpMail.Send(mail);
                  }
                  catch(Exception ex)
                  {
                        Response.Write("<b>Exception Occured:</b>   " +ex);
                  }
                  finally
                  {
                        Response.Write("Your E-mail has been sent sucessfully");
                  }

                  // Uploaded file deleted after sending e-mail

                  File.Delete(strdir+strfilename);
            }

Regards
Caz
BeginningWebDesignAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ANamikaCommented:
when are you getting teh error: on page load on submit click
ANamikaCommented:
In page load it might be because there may not be any value selected for ddlCountry etc.(assuming it is dropdown) and you are trying to access selecteditem.text
BeginningWebDesignAuthor Commented:
Hi ANamika

On submit click

Caz
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

ANamikaCommented:
by any chance have you debugged and have any idea on which lien teh error is coming.

The error message is generic and can come on any line of your code. We can pinpoint teh defect, if we know what line is triggereing the error
BeginningWebDesignAuthor Commented:
Hi ANamika

It appears to be this line
string strfilename = Path.GetFileName(txtFile.PostedFile.FileName);

Caz
ANamikaCommented:
TRY ADDING THE VALIDATION
if (txtFile.PostedFile != null)

before the line
txtFile.PostedFile.SaveAs(strdir+strfilename);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BeginningWebDesignAuthor Commented:
Thanks Anamika

Sorted

Caz
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.