Why is my code returning a Null Object Refrence error when using WatIn?
Posted
by
Fuzz Evans
on Stack Overflow
See other posts from Stack Overflow
or by Fuzz Evans
Published on 2011-11-19T14:57:35Z
Indexed on
2011/11/19
17:51 UTC
Read the original article
Hit count: 363
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]
© Stack Overflow or respective owner