[.net] Whats wrong with my code?
Hi, could someone pls tell me what is wrong with my code?
what i'm trying to do is to read lines from a pre-edited text file and store them into an string Array - gameMapArray, then send off each line to LoadLine method and extract each char out of the string to do the switch statement check.
public void InitializeTiles()
{
gameBitmapFile = new StreamReader(@"..\..\bitmapfield.txt");
mapSize = Convert.ToInt32(gameBitmapFile.ReadLine());
gameMapArray = new string[mapSize];
for (int i = 1; i < mapSize; i++)
{
gameMapArray[mapSize - i] = gameBitmapFile.ReadLine();
}
gameBitmapFile.Close();
for (int i = 0; i < gameMapArray.Length; i++)
{
LoadLine( gameMapArray);
}
}
public void LoadLine(string lines)
{
for (int i = 0; i < 12 ; i++)
{
switch ( lines) <<------ error msg on this line
{
case '1':
tiles.mapRect = tiles.tileRect[0, 1];
break;
case '2':
tiles.mapRect = tiles.tileRect[0, 2];
break;
.
.
default:
break;
}
}
}
Quote:
public void LoadLine(string lines)
{
for (int i = 0; i < 12 ; i++)
{
switch ( lines) <<------ error msg on this line
Looks like the variable "lines" holds a string value and you are filling it with an integer (int i) as an array.
Quote:Original post by IronGryphonYou can access the individual characters from a string like that in C#/.NET, if I remember correctly...Quote:
public void LoadLine(string lines)
{
for (int i = 0; i < 12 ; i++)
{
switch ( lines) <<------ error msg on this line
Looks like the variable "lines" holds a string value and you are filling it with an integer (int i)
The error msg it generated: NullReferenceException, use "new" keyword to create the instance.
for (int i = 0; i < 12 ; i++){ switch ( lines) <<------ error msg on this line{
Do you need to verify that i actualy exists in the string?
A couple of things that would help us.
1. Could you post the data file you are loading
2. What line in the data file does it fail on?
Just a guess off the top of my head... are you sure there are 12 characters on each line of that file? I thought you would have got a ArrayOutOfBoundsException (or whatever its called) for that though...
1. Could you post the data file you are loading
2. What line in the data file does it fail on?
Just a guess off the top of my head... are you sure there are 12 characters on each line of that file? I thought you would have got a ArrayOutOfBoundsException (or whatever its called) for that though...
I was all set to give a smartarse bounds-checking answer, but a NullReferenceException? That means the string is null, not just too short. The only explanation for that is that you tried to read lines that weren't there in your file (ReadLine returns null if you are at the end of the file). In turn, that means that your file didn't contain as many lines as you thought it did, so check the number you typed on its first line is correct.
As an aside, I'd think a better way to do this would be
... removing the need for the length to be specified at all. Since you're loading the whole array anyway I very much doubt your file is big enough to cause any memory headaches.
As an aside, I'd think a better way to do this would be
gameBitmapFile = new StreamReader(@"..\..\bitmapfield.txt");gameMapArray = gameBitmapFile.ReadToEnd().Split('\n');gameBitmapFile.Close();
... removing the need for the length to be specified at all. Since you're loading the whole array anyway I very much doubt your file is big enough to cause any memory headaches.
well, the string is not null. instead of doing: switch(lines), i tried to print the lines string to the screen and it showed up correctly, exactly as whats in the txt file. i'm really lost.
Quote:
for (int i = 0; i < 12 ; i++)
{
switch ( lines) <<------ error msg on this line
There aren't 12 lines.
Or maybe you want from 0 to 11 (which is 12 iterations)
In any event, here is a better construct that will work for any number of lines passed in.
for (int i = 0; i < lines.Length; i++)
{
switch ( lines)
...
Also, when you fill the array, you might want to start at zero instead of 1
for (int i = 1; i < mapSize; i++)
{
gameMapArray[mapSize - i] = gameBitmapFile.ReadLine();
}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement