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 8013705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:42:14+00:00 2026-06-04T19:42:14+00:00

I added a .txt file to the \bin\Debug folder, and am trying to open

  • 0

I added a .txt file to the \bin\Debug folder, and am trying to open it and read from it like so:

using (StreamReader reader = File.OpenText("Credentials.txt")) {
    string line = null;
    do {
        line = reader.ReadLine();
        if (line.Contains("host=")) {
    . . .

But, although the file is there, when I get to the “ReadLine()” line, it stops dead in its tracks with:

System.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.

UPDATE

I had to change it from “do...while (line != null);” to “while (! reader.EndOfStream)“

  • 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-06-04T19:42:17+00:00Added an answer on June 4, 2026 at 7:42 pm

    That doesn’t make sense. I’m not aware of any scenario in which File.OpenText returns successfully and the return value is null. If File.OpenText can not find the file, it throws a FileNotFoundException.

    Are you sure that the line you state is actually the line throwing the exception? Please check the stack trace carefully.

    Note that your loop is in the wrong order. You’re calling StreamReader.ReadLine and then checking if the line contains “host=”. But it could be the case that you’ve reached the end of the stream in which case line will be null and you’ll throw. One way to rewrite your loop is to say

    while ((line = reader.ReadLine()) != null) {
        if (line.Contains("host=")) {
        }
    }
    

    I’m not saying this is the best way (I don’t think it is), but it is a common idiom in C#.

    If you insist that you’re correct, please print the output of

    Console.WriteLine(reader == null);
    

    which you will place immediately before the

    line = reader.ReadLine();
    

    line.

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

Sidebar

Related Questions

I have a file paylines.txt added inside the folder named TextFiles which resides inside
I am trying to read a file (made a simple .txt file on my
i would to import the last modified txt file from a directory using a
I have a file that is getting added to remotely (file.txt). From SSH, I
I'd like to add new line with text to my date.txt file, but instead
I want to read the first line of a text file that I added
I have added an open file dialog box to my client application so that
Im generating a plain txt file from Physics editor. It contains all the vertices
I have an app in which I parse a .txt file from a URL
I have a data set which I read from my a .csv file. After

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.