System.ArgumentOutOfRangeException: Index was out of range
-
/*I have elimated some of the code in order to make it short and get straight to the point. I am noticing that my program will NOT process any of the non regular ascii characters (decimal 161 to 255 || hex A1 to FF) without the error below. All other regular ascii characters are process ok without errors.
OUTPUT::::::::::BELOWcount = 1
count = 3
count = 6Unhandled Exception: System.ArgumentOutOfRangeException: Index was out of range.
Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Collections.BitArray.Get(Int32 index)
at Applica.Program.Main(String[] args) in C:\Documents and Settings\shampro\D
esktop\Program.cs:line 55
Press any key to continue . . .
*/using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Applica
{
class Program
{
static void Main(string[] args)
{
DirectoryInfo da = new DirectoryInfo("C:\\Folder7");
FileInfo[] Arr = da.GetFiles();
if (Arr.Length == 0)
{
throw new InvalidOperationException("No files found.");
}
FileInfo ap = Arr[Arr.Length - 1];
ulong Totbyte = (ulong)ap.Length;
string filePath = ap.FullName;
string temPath = Path.GetTempFileName();
byte[] data = File.ReadAllBytes(filePath);
File.WriteAllBytes(temPath, data);
byte[] dataa = new byte[1];
for (ulong counter = 0; counter < Totbyte; counter++)
{
dataa[0] = data[counter];
BitArray bits = new BitArray(dataa);
for (int count = 0; count < bits.Length;)
{
if (count != 7)
{
if (bits[count] == false && bits[count + 1] == true)
count++;
if (bits[count] == false && bits[count + 1] == false)
count++;
if (bits[count] == true && bits[count + 1] == true)/////line 55 error
count++;
if (bits[count] == true && bits[count + 1] == false)
count++;
Console.WriteLine("count = {0}", count);
}
}
}
} -
/*I have elimated some of the code in order to make it short and get straight to the point. I am noticing that my program will NOT process any of the non regular ascii characters (decimal 161 to 255 || hex A1 to FF) without the error below. All other regular ascii characters are process ok without errors.
OUTPUT::::::::::BELOWcount = 1
count = 3
count = 6Unhandled Exception: System.ArgumentOutOfRangeException: Index was out of range.
Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Collections.BitArray.Get(Int32 index)
at Applica.Program.Main(String[] args) in C:\Documents and Settings\shampro\D
esktop\Program.cs:line 55
Press any key to continue . . .
*/using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Applica
{
class Program
{
static void Main(string[] args)
{
DirectoryInfo da = new DirectoryInfo("C:\\Folder7");
FileInfo[] Arr = da.GetFiles();
if (Arr.Length == 0)
{
throw new InvalidOperationException("No files found.");
}
FileInfo ap = Arr[Arr.Length - 1];
ulong Totbyte = (ulong)ap.Length;
string filePath = ap.FullName;
string temPath = Path.GetTempFileName();
byte[] data = File.ReadAllBytes(filePath);
File.WriteAllBytes(temPath, data);
byte[] dataa = new byte[1];
for (ulong counter = 0; counter < Totbyte; counter++)
{
dataa[0] = data[counter];
BitArray bits = new BitArray(dataa);
for (int count = 0; count < bits.Length;)
{
if (count != 7)
{
if (bits[count] == false && bits[count + 1] == true)
count++;
if (bits[count] == false && bits[count + 1] == false)
count++;
if (bits[count] == true && bits[count + 1] == true)/////line 55 error
count++;
if (bits[count] == true && bits[count + 1] == false)
count++;
Console.WriteLine("count = {0}", count);
}
}
}
}When your loop is in its last iteration,
count == bits.Length - 1
, so guess whatcount + 1
is equal to?The difficult we do right away... ...the impossible takes slightly longer.
-
When your loop is in its last iteration,
count == bits.Length - 1
, so guess whatcount + 1
is equal to?The difficult we do right away... ...the impossible takes slightly longer.
No its not. Look at the output. I ran the entire alphabet with no problem, but soon as I try to run just one or any special ascii characters like µ it gives an error.
-
No its not. Look at the output. I ran the entire alphabet with no problem, but soon as I try to run just one or any special ascii characters like µ it gives an error.
OK, I might be wrong. I didn't trace the code, I only gave a quick look. My apologies.
The difficult we do right away... ...the impossible takes slightly longer.
-
/*I have elimated some of the code in order to make it short and get straight to the point. I am noticing that my program will NOT process any of the non regular ascii characters (decimal 161 to 255 || hex A1 to FF) without the error below. All other regular ascii characters are process ok without errors.
OUTPUT::::::::::BELOWcount = 1
count = 3
count = 6Unhandled Exception: System.ArgumentOutOfRangeException: Index was out of range.
Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Collections.BitArray.Get(Int32 index)
at Applica.Program.Main(String[] args) in C:\Documents and Settings\shampro\D
esktop\Program.cs:line 55
Press any key to continue . . .
*/using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Applica
{
class Program
{
static void Main(string[] args)
{
DirectoryInfo da = new DirectoryInfo("C:\\Folder7");
FileInfo[] Arr = da.GetFiles();
if (Arr.Length == 0)
{
throw new InvalidOperationException("No files found.");
}
FileInfo ap = Arr[Arr.Length - 1];
ulong Totbyte = (ulong)ap.Length;
string filePath = ap.FullName;
string temPath = Path.GetTempFileName();
byte[] data = File.ReadAllBytes(filePath);
File.WriteAllBytes(temPath, data);
byte[] dataa = new byte[1];
for (ulong counter = 0; counter < Totbyte; counter++)
{
dataa[0] = data[counter];
BitArray bits = new BitArray(dataa);
for (int count = 0; count < bits.Length;)
{
if (count != 7)
{
if (bits[count] == false && bits[count + 1] == true)
count++;
if (bits[count] == false && bits[count + 1] == false)
count++;
if (bits[count] == true && bits[count + 1] == true)/////line 55 error
count++;
if (bits[count] == true && bits[count + 1] == false)
count++;
Console.WriteLine("count = {0}", count);
}
}
}
}You're incrementing the
count
variable within the loop, and then trying to access the item at the new incremented index without checking whether it's in range. Once thecount
is out of range, the indexer will throw anIndexOutOfRangeException
. Add a test to the start of eachif
block to ensure that the index is still in range:if (count < bits.Length - 1 && bits[count] == false && bits[count + 1] == true)
count++;if (count < bits.Length - 1 && bits[count] == false && bits[count + 1] == false)
count++;if (count < bits.Length - 1 && bits[count] == true && bits[count + 1] == true)
count++;if (count < bits.Length - 1 && bits[count] == true && bits[count + 1] == false)
count++;
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
You're incrementing the
count
variable within the loop, and then trying to access the item at the new incremented index without checking whether it's in range. Once thecount
is out of range, the indexer will throw anIndexOutOfRangeException
. Add a test to the start of eachif
block to ensure that the index is still in range:if (count < bits.Length - 1 && bits[count] == false && bits[count + 1] == true)
count++;if (count < bits.Length - 1 && bits[count] == false && bits[count + 1] == false)
count++;if (count < bits.Length - 1 && bits[count] == true && bits[count + 1] == true)
count++;if (count < bits.Length - 1 && bits[count] == true && bits[count + 1] == false)
count++;
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Your solution did not produce an error, but it I still cannot understand why it allowed to process the ENTIRE alphabet in one shot. It would not even allow me to process one(1) special character with producing an error. Can you please explain why this? I simply cannot wrap my thoughts around it.
-
Your solution did not produce an error, but it I still cannot understand why it allowed to process the ENTIRE alphabet in one shot. It would not even allow me to process one(1) special character with producing an error. Can you please explain why this? I simply cannot wrap my thoughts around it.
For ASCII characters, the byte values will be 127 or less. These bytes do not have the most significant bit set. For byte values greater than 127, the most significant bit is set. Due to the way you've constructed your loop, if the most-significant bit is set, you will increment
count
out of range within the loop, and without the range checks in place, you'll get an exception.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
/*I have elimated some of the code in order to make it short and get straight to the point. I am noticing that my program will NOT process any of the non regular ascii characters (decimal 161 to 255 || hex A1 to FF) without the error below. All other regular ascii characters are process ok without errors.
OUTPUT::::::::::BELOWcount = 1
count = 3
count = 6Unhandled Exception: System.ArgumentOutOfRangeException: Index was out of range.
Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Collections.BitArray.Get(Int32 index)
at Applica.Program.Main(String[] args) in C:\Documents and Settings\shampro\D
esktop\Program.cs:line 55
Press any key to continue . . .
*/using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Applica
{
class Program
{
static void Main(string[] args)
{
DirectoryInfo da = new DirectoryInfo("C:\\Folder7");
FileInfo[] Arr = da.GetFiles();
if (Arr.Length == 0)
{
throw new InvalidOperationException("No files found.");
}
FileInfo ap = Arr[Arr.Length - 1];
ulong Totbyte = (ulong)ap.Length;
string filePath = ap.FullName;
string temPath = Path.GetTempFileName();
byte[] data = File.ReadAllBytes(filePath);
File.WriteAllBytes(temPath, data);
byte[] dataa = new byte[1];
for (ulong counter = 0; counter < Totbyte; counter++)
{
dataa[0] = data[counter];
BitArray bits = new BitArray(dataa);
for (int count = 0; count < bits.Length;)
{
if (count != 7)
{
if (bits[count] == false && bits[count + 1] == true)
count++;
if (bits[count] == false && bits[count + 1] == false)
count++;
if (bits[count] == true && bits[count + 1] == true)/////line 55 error
count++;
if (bits[count] == true && bits[count + 1] == false)
count++;
Console.WriteLine("count = {0}", count);
}
}
}
}The best part about this is that if you make it work, i.e. don't increment count before checking values again, the entire content of the if statement simplifies to
count++;
Console.WriteLine("count = {0}", count);... because you are testing all combinations. I'm not sure what you're trying to do but I'm sure the code is not doing it.
-
For ASCII characters, the byte values will be 127 or less. These bytes do not have the most significant bit set. For byte values greater than 127, the most significant bit is set. Due to the way you've constructed your loop, if the most-significant bit is set, you will increment
count
out of range within the loop, and without the range checks in place, you'll get an exception.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
I looked at my program for two (2) days straight. I looked at it night and day and could not make heads or tails of the problem. At one point I thought I was going to lose my mind. I am not sure I that I understand your explanation completely, but thanks. I really needed some sort of explanation so I put my mind at rest. Thanks.
-
The best part about this is that if you make it work, i.e. don't increment count before checking values again, the entire content of the if statement simplifies to
count++;
Console.WriteLine("count = {0}", count);... because you are testing all combinations. I'm not sure what you're trying to do but I'm sure the code is not doing it.
I was also reading online and saw that many people had this same problem. One of the experts said it was a dot.net error and that people should contact Microsoft tech support for a fix.
-
I was also reading online and saw that many people had this same problem. One of the experts said it was a dot.net error and that people should contact Microsoft tech support for a fix.
This is indeed a very strange problem to experience. I ran the entire English alphabet all at once and did not get an error. I ran only one special character and got errors every time.
-
The best part about this is that if you make it work, i.e. don't increment count before checking values again, the entire content of the if statement simplifies to
count++;
Console.WriteLine("count = {0}", count);... because you are testing all combinations. I'm not sure what you're trying to do but I'm sure the code is not doing it.
That's what I thought at first, but it's not quite that simple. If condition 1 is met, you move to the next index, and test condition 2 against the new index. Therefore, it's possible to increment the indexer at least three times within the same iteration of the loop. For example:
0 1 1 0
^(condition 1 is met)
0 1 1 0
^(condition 2 is not met)
(condition 3 is met)0 1 1 0
^(condition 4 is met)
0 1 1 0
^
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
That's what I thought at first, but it's not quite that simple. If condition 1 is met, you move to the next index, and test condition 2 against the new index. Therefore, it's possible to increment the indexer at least three times within the same iteration of the loop. For example:
0 1 1 0
^(condition 1 is met)
0 1 1 0
^(condition 2 is not met)
(condition 3 is met)0 1 1 0
^(condition 4 is met)
0 1 1 0
^
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer