6 Replies - 547 Views - Last Post: 19 July 2009 - 07:17 AM Rate Topic: -----

#1 Bloobury  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 18-July 09

NullReferenceException Help! D:

Posted 18 July 2009 - 01:34 PM

Hello! I'm having an issue with the NullReferenceException.
Well here's my code.
The error appears at string curex = Extensions.GetValue(k).ToString();
I know the arrary Extensions isn't null (I think, I tested it, and it proved to have things in it!)
foreach (string f in Directory.GetFiles(MainLocation))
			{
				char[] delimiterChars = { '\\' };
				char[] others = { '.' };
				string[] a = f.ToString().Split(delimiterChars);
				string[] b = a.GetValue(a.Length - 1).ToString().Split(others);
				string ex = b.GetValue(0).ToString();
				string curfile = MainLocation + "//"+ b.GetValue(0).ToString() + "." + b.GetValue(1).ToString();
				for (int k = 0; k < Extensions.Length; k++)
				{
					char[] remove = { '$' };
					string curex = Extensions.GetValue(k).ToString();
					string[] s = curex.Split(remove);
					string title = s.GetValue(0).ToString();
					string searchex = s.GetValue(1).ToString();
					if (searchex == ex)
					{
						MessageBox.Show(curfile + "\n" + ex + "\n" + searchex);
					}
				}
			}



This is the globar var I am using.
string[] Extensions = new string[1000000];
//Big number I know :p


This is where I am adding values to the arrary.
The MessageBox seems to give me a value;
while ((line = sr.ReadLine()) != null)
					{
						if (line == "") goto Finish;
						if (x == 0) Locations.SetValue(FileName + "$" + line.ToString(), Locations.Length - 1);
						if (x != 0)
						{
							Extensions.SetValue(FileName + "$" + line.ToLower().ToString(), Extensions.Length - 1);
							MessageBox.Show(Extensions.GetValue(Extensions.Length - 1).ToString());
						}
						x++;
					}


This post has been edited by Bloobury: 18 July 2009 - 01:51 PM


Is This A Good Question/Topic? 0
  • +

Replies To: NullReferenceException Help! D:

#2 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: NullReferenceException Help! D:

Posted 18 July 2009 - 01:45 PM

Well, your case probably is something like this:
You have Extensions array, I don't know the type so lets just say it's MyExtension. The declaration is this:

MyExtension[] Extensions = new MyExtension[3];



Okay, Extensions isn't null anymore. But this is how its cells look like:

Extensions[0] = null;
Extensions[1] = null;
Extensions[2] = null;



And that's where it crashes, you try to get Extensions.GetValue(k).ToString() but Extensions.GetValue(k) = null;

If I'm wrong, please provide your declaration for the Extensions array and also where you set the value of its cells.

Hope this helped.
Was This Post Helpful? 0
  • +
  • -

#3 Bloobury  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 18-July 09

Re: NullReferenceException Help! D:

Posted 18 July 2009 - 01:53 PM

Hey, I updated it, and thanks for your help, I understand arrays a bit more. :). However, I still don't see what I'm doing wrong.
Was This Post Helpful? 0
  • +
  • -

#4 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: NullReferenceException Help! D:

Posted 18 July 2009 - 02:04 PM

Okay, the same happens what I said. After you have read the file (I assume it's a file because of sr.ReadLine(), anyways), your array looks like this:

Extensions[0] = "line 1...";
Extensions[1] = "line 2...";
Extensions[2] = "line 3...";
...
Extensions[40] = "line 40...";
Extensions[41] = "LAST LINE";
Extensions[42] = null;
Extensions[43] = null;
...
Extensions[1000000] = null;



But in your for loop you have Extensions.Length which is 1000000 because the Length property doesn't check if a value actually exists, it just returns the array's size you have declared.

So in the for loop the variable k's value hits 41 which still exists and then at 42 which is null, it crashes.

So, what can you do.

This is one option:
				for (int k = 0; k < Extensions.Length; k++)
				{
					// check if the value is null
					if (Extensions[k] == null) {
						continue; // if null, continue for loop
					}

					char[] remove = { '$' };
					string curex = Extensions.GetValue(k).ToString();
					string[] s = curex.Split(remove);
					string title = s.GetValue(0).ToString();
					string searchex = s.GetValue(1).ToString();
					if (searchex == ex)
					{
						MessageBox.Show(curfile + "\n" + ex + "\n" + searchex);
					}
				}
			}



That if statement will prevent it for crashing on null values.

You could also use List<string> instead of array but that might chance your program a bit too much and might confuse you so you might be better stick to arrays. You can learn Lists later.

This post has been edited by janne_panne: 18 July 2009 - 02:04 PM

Was This Post Helpful? 1
  • +
  • -

#5 Bloobury  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 18-July 09

Re: NullReferenceException Help! D:

Posted 18 July 2009 - 02:06 PM

Thanks! a bunch :) You're awesome hehe.
Was This Post Helpful? 0
  • +
  • -

#6 sknake  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 32
  • Joined: 15-July 09

Re: NullReferenceException Help! D:

Posted 19 July 2009 - 05:57 AM

Good god man, don't declare an array that large! Use a generic List<string>. As a rule of thumb generic lists will handle memory allocation better than an array when you are doing things like this.
Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: NullReferenceException Help! D:

Posted 19 July 2009 - 07:17 AM

I agree with sknake. Think of generic lists as "dynamic arrays", with more functionality. If you don't know the exact amount of items you need in the array, then use a generic list.

Hell, even if you do know the amount of items, generic lists still give you more functionality than working with arrays does.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1