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

  • Home
  • SEARCH
  • 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 6820053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:22:44+00:00 2026-05-26T21:22:44+00:00

I keep getting a Null Object Refrence Error, but can’t tell why. I have

  • 0

I keep getting a Null Object Refrence Error, but can’t tell why.
I have a CSV file that contains 100 urls. The file is read into an array called “lines”.

public partial class Form1 : System.Windows.Forms.Form
{
    string[] lines;
    public Form1() ...    

private void ReadLinksIntoMemory()
    {
        //this reads the chosen csv file into our "lines" array
        //and splits on comma's and new lines to create new objects within the array
        using (StreamReader sr = new StreamReader(@"C:\temp.csv"))
        {
            //reads everything in our csv into 1 long line
            string fileContents = sr.ReadToEnd();

            //splits the 1 long line read in into multiple objects of the lines array
           lines = fileContents.Split(new string[] { ",", Environment.NewLine },
                StringSplitOptions.RemoveEmptyEntries);

            sr.Dispose();
        }
    }

The next part is where I get the null object error. When I try to use WatIn to go to the first item in the lines array it says I’m referencing a null object.

private void GoToEditLinks()
    {                        
        for (int i = 0; i < lines.Length; i++)
        {
            //go to each link sequentially
            myIE.GoTo(lines[i].ToString());

            //sleep so we can make sure the page loads
            System.Threading.Thread.Sleep(5000);                  
        }
    }

When I debug the code it says that the GoTo request calls lines which is null.

It seems like I need to declare the array, but don’t I need to tell it an exact size to do that? Example:

lines = new string[10]

I thought I could use the lines.Length to tell it how big to make the array but that didn’t work. What is weird to me is I can use the following code without problem:

//returns the accurate number of urls that were in the CSV we read in earlier
txtbx1.text = lines.Length;

//or
//this returns the last entry in the csv, as well as the last entry in the array
TextBox2.Text = lines[lines.Length - 1];

I am confused why the array clearly has items in it, they can be called to fill a text box, but when I try to call them in my for loop it says its a null reference?

UPDATE:
By placing my cursor on both calls to lines and pressing f12 I find they both go to the same instance. The thought next is that I am not calling ReadLinksIntoMemory in time, below is my code:

private void button1_Click(object sender, EventArgs e)
    {
        button1.Enabled = false;

        ReadLinksIntoMemory();

        GoToEditLinks();

        button1.Enabled = true;

    }

Unless I’m mistaken the code says that the ReadLinksIntoMemory method must complete before GoToEditLinks can be called? If ReadLinksIntoMemory didn’t finish in time I shouldn’t be able to fill my text boxes with the lines array length and/or last entry.

UPDATE: Stepping into the method GoToEditLinks() I see that lines is null before it calls:

myIE.GoTo(lines[i]);

but when it hits the goto command the value changes from null to the url it is suppose to go to, but at that same time it gives me the null object error?

UPDATE:
I added a IsNullOrEmpty check method and lines array passes it without any issue. I’m beginning to think it is an issue with WatIn and the myIE.GoTo command.

I think this is the stack trace/call stack?

Program.exe!Program.Form1.GoToEditLinks() Line 284  C#
Program.exe!Program.Form1.button1_Click(object sender, System.EventArgs e) Line 191 + 0x8 bytes C#
[External Code] 
Program.exe!Program.Program.Main() Line 18 + 0x1d bytes C#
[External Code] 

SOLUTION:
It turns out the issue WAS WatIn. Under normal circumstances I have initialized the myIE object but in this instance I was doing a shorthand test on those two methods which meant that the myIE object had not yet been created! I ran the method to initialize the myIE object first and it worked. Thank you everyone for helping me work through this.

  • 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-26T21:22:44+00:00Added an answer on May 26, 2026 at 9:22 pm

    The code in the first 2 samples looks OK, provided ReadLinksIntoMemory() runs first. It creates the lines array.

    Try this:

    1. put your cursor on lines inside ReadLinksIntoMemory, pres F12. It should go to the declaration at the top of the class.

    2. put your cursor on lines inside GoToEditLink. It should take you to the same declaration.

    Either you’ll find that there are 2 declaration for lines or that you don’t call ReadLinksIntoMemory() on time.

    You should not need lines = new string[10] .


    Step 2: put breakpoints at the start of both ReadLinksIntoMemory and GoToEditLink. Make sure the first one breaks first.

    Then inside GoToEditLink, carefully check lines, lines.Length and (some of) its contents.

    This variation of String.Split() should not produce null entries but still:

    for (int i = 0; i < lines.Length; i++)
    {
      //go to each link sequentially
      string line = lines[i]; 
      myIE.GoTo(line);   // set a breakpoint here with the Condition `line == null`. 
    
      ...
    }
    

    Note that there is no reason for .ToString() on an element from a string[]

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

Sidebar

Related Questions

I keep getting tasks that are above my skill level. How can I address this without coming accross as grossly incompetent?
I keep getting the following error: Cannot access a disposed object. Object name: 'AdoTransaction'.
I am trying to pass a JSON object to .getJSON but I keep getting
When my threads complete, I keep getting exceptions thrown. I can catch them, but
I keep getting a null exception at the ; below. The ApiUsername & ApiPassword
Keep getting this error after inserting a subdatasheet into a query and trying to
I keep getting a PermGen error on my Tomcat 6 server. I know what
I keep getting this NPE in my application and I can't seem to get
I keep getting an SWTException that says Invalid Thread Access when I try and
I have a JSON object that looks like this. [ { id : 23,

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.