C
C#2y ago
morry329#

❔ ✅ IndexOutOfRangeException

I just tried to code out a simple app that reads a .csv file (as per screenshot) and displays the content in the columns "state" and "city" . My Main points out an IndexOutOfRangeException, so something is out of the range. But I am lost to find it out now.
static void Main(string[] args)
{
var codeSearch = new ZIPCodeSearch();
codeSearch.Build(new StreamReader(args[0], Encoding.Default)); //ERROR HERE

Console.WriteLine($"Anwendung ZIPCodeSearch");

string value;
while ((value = Console.ReadLine()) != "")
{
var scanned = codeSearch.Classify(value);
Console.WriteLine($"Stadt: {codeSearch.Classify(value)} unbekannt");
Console.WriteLine($"Land:{codeSearch.Classify(value)}");
}
}
static void Main(string[] args)
{
var codeSearch = new ZIPCodeSearch();
codeSearch.Build(new StreamReader(args[0], Encoding.Default)); //ERROR HERE

Console.WriteLine($"Anwendung ZIPCodeSearch");

string value;
while ((value = Console.ReadLine()) != "")
{
var scanned = codeSearch.Classify(value);
Console.WriteLine($"Stadt: {codeSearch.Classify(value)} unbekannt");
Console.WriteLine($"Land:{codeSearch.Classify(value)}");
}
}
Could anyone point me in the right direction? My other classes for this project is here - FYI: https://github.com/morry239/HashTableRedBook/blob/master/ZIPCodeSearch/ZIPCodeSearch.cs
GitHub
HashTableRedBook/ZIPCodeSearch.cs at master · morry239/HashTableRed...
Contribute to morry239/HashTableRedBook development by creating an account on GitHub.
13 Replies
TheBoxyBear
TheBoxyBear2y ago
If you don't pass any command line arguments, args will be empty You can define arguments when debugging in the project properties
morry329#
morry329#OP2y ago
If not, shall I enter the valid string into somewhere in the code? The debugging showed that I have some null strings as per screenshot
morry329#
morry329#OP2y ago
@TheBoxyBear
TheBoxyBear
TheBoxyBear2y ago
This Though this only when debugging. Once released, the user still has to provide the arguments to the executable so you still need to check if the array is empty The debug option is Rider adding the arguments automatically when running the program.
morry329#
morry329#OP2y ago
Ok I think my problem is now find out what kind of the argument I shall enter into (something along the lines of "if array is null == do this" right?) I have installed the EzArgs plugin mentioned by the JetBrains article and did this:
morry329#
morry329#OP2y ago
morry329#
morry329#OP2y ago
It still triggers the Index out of range", so I think my query here is wrong
TheBoxyBear
TheBoxyBear2y ago
The arguments are the values you want in the args array The length check needs to be in your code Checking the length, not if it's null
morry329#
morry329#OP2y ago
So this is my progress so far
static void Main(string[] args)
{

var codeSearch = new ZIPCodeSearch();
string filePath = @"/Users/myusername/Desktop/geo-data.csv";

try
{
using (StreamReader streamReader = new StreamReader(filePath))
{
while ((filePath = streamReader.ReadLine()) != null)
{
codeSearch.Build(new StreamReader (new FileStream(filePath, FileMode.Open, FileAccess.Read)));

}
}
}
catch(Exception exp)
{
Console.WriteLine(exp.Message);
}
Console.WriteLine($"ZIPCodeSearch-App");

string value;
while ((value = Console.ReadLine()) != "")
{
var scanned = codeSearch.Classify(value);
Console.WriteLine($"Stadt: {codeSearch.Classify(value)} unbekannt");
Console.WriteLine($"Land:{codeSearch.Classify(value)}");
}
}
static void Main(string[] args)
{

var codeSearch = new ZIPCodeSearch();
string filePath = @"/Users/myusername/Desktop/geo-data.csv";

try
{
using (StreamReader streamReader = new StreamReader(filePath))
{
while ((filePath = streamReader.ReadLine()) != null)
{
codeSearch.Build(new StreamReader (new FileStream(filePath, FileMode.Open, FileAccess.Read)));

}
}
}
catch(Exception exp)
{
Console.WriteLine(exp.Message);
}
Console.WriteLine($"ZIPCodeSearch-App");

string value;
while ((value = Console.ReadLine()) != "")
{
var scanned = codeSearch.Classify(value);
Console.WriteLine($"Stadt: {codeSearch.Classify(value)} unbekannt");
Console.WriteLine($"Land:{codeSearch.Classify(value)}");
}
}
` I still don't know how to add the length check in here (maybe something like "long length = streamReader.BaseStream.Length;"). But this code gave me some another error I have never seen yet:
Could not find file '/Users/myusername/RiderProjects/ZIPCodeSuche_RotesBuch/ZIPCodeSuche_RotesBuch/bin/Debug/net6.0/state_fips,state,state_abbr,zipcode,county,city'."""
Could not find file '/Users/myusername/RiderProjects/ZIPCodeSuche_RotesBuch/ZIPCodeSuche_RotesBuch/bin/Debug/net6.0/state_fips,state,state_abbr,zipcode,county,city'."""
` So I think the problem shifted from the index out of range to something else (most likely the invalid file path referenced somewhere in the code). What do you think? @TheBoxyBear
TheBoxyBear
TheBoxyBear2y ago
Without a drive letter in the path, the path is relative to the location of the executable For the length check, it's as simple as checking args.Length > 0
morry329#
morry329#OP2y ago
I am on MacBook, so my computer does not have a drive letter in the path(C:, F: you mean any of these right?). I have made a few more changes to my code:
static void Main(string[] args)
{

var codeSearch = new ZIPCodeSearch();
string filePath = @"/Users/myname
/Desktop/geo-data.csv";

try
{
//using (StreamReader streamReader = new StreamReader(filePath))
using (StreamReader streamReader = new StreamReader(filePath))
{
while ((filePath = streamReader.ReadLine()) != null && args.Length > 0)
{
codeSearch.Build(new StreamReader(args[0], Encoding.Default));

}
}
}
catch(Exception exp)
{
Console.WriteLine(exp.Message);
}
Console.WriteLine($"ZIPCodeSearch-App");

string value;
while ((value = Console.ReadLine()) != "")
{
var scanned = codeSearch.Classify(value);
Console.WriteLine($"Stadt: {codeSearch.Classify(value)} unbekannt");
Console.WriteLine($"Land:{codeSearch.Classify(value)}");
}
}
static void Main(string[] args)
{

var codeSearch = new ZIPCodeSearch();
string filePath = @"/Users/myname
/Desktop/geo-data.csv";

try
{
//using (StreamReader streamReader = new StreamReader(filePath))
using (StreamReader streamReader = new StreamReader(filePath))
{
while ((filePath = streamReader.ReadLine()) != null && args.Length > 0)
{
codeSearch.Build(new StreamReader(args[0], Encoding.Default));

}
}
}
catch(Exception exp)
{
Console.WriteLine(exp.Message);
}
Console.WriteLine($"ZIPCodeSearch-App");

string value;
while ((value = Console.ReadLine()) != "")
{
var scanned = codeSearch.Classify(value);
Console.WriteLine($"Stadt: {codeSearch.Classify(value)} unbekannt");
Console.WriteLine($"Land:{codeSearch.Classify(value)}");
}
}
The index out of range error is gone now, so your advice to add length check worked (I think). This app still has problems, but I will set up another thread separately. Thank you so much for the guidance 🙂
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server